• 找Bug


    https://leetcode-cn.com/problems/ransom-note/

    func canConstruct(ransomNote string, magazine string) bool {
    	m := len(ransomNote)
    	n := len(magazine)
    	if m > n {
    		return false
    	}
    	index := []int{}
    	for i := 0; i < m; i++ {
    		ok := false
    		for ii := 0; ii < n; ii++ {
    			if ransomNote[i] == magazine[ii] {
    				j := len(index)
    				for iii := 0; iii < j; iii++ {
    					if ii != index[iii] {
    						index = append(index, ii)
    						ok = true
    						break
    					}
    				}
    			}
    			if ok {
    				break
    			}
    		}
    		if !ok {
    			return false
    		}
    	}
    	return true
    }
    

      

    对空集合的遍历初Bug

    func canConstruct(ransomNote string, magazine string) bool {
    	m := len(ransomNote)
    	n := len(magazine)
    	if m > n {
    		return false
    	}
    	index := []int{}
    	for i := 0; i < m; i++ {
    		ok := false
    		for ii := 0; ii < n; ii++ {
    			if ransomNote[i] == magazine[ii] {
    				j := len(index)
    				if j == 0 {
    					index = append(index, ii)
    					ok = true
    					break
    				} else {
    					for iii := 0; iii < j; iii++ {
    						if ii != index[iii] {
    							index = append(index, ii)
    							ok = true
    							break
    						}
    					}
    				}
    
    			}
    			if ok {
    				break
    			}
    		}
    		if !ok {
    			return false
    		}
    	}
    	return true
    }
    

      遍历循环中逻辑的有效范围Bug

    func canConstruct(ransomNote string, magazine string) bool {
    	m := len(ransomNote)
    	n := len(magazine)
    	if m > n {
    		return false
    	}
    	index := []int{}
    	for i := 0; i < m; i++ {
    		ok := false
    		for ii := 0; ii < n; ii++ {
    			if ransomNote[i] == magazine[ii] {
    				j := len(index)
    				if j == 0 {
    					index = append(index, ii)
    					ok = true
    					break
    				} else {
    					for iii := 0; iii < j; iii++ {
    						if ii != index[iii] {
    							index = append(index, ii)
    							ok = true
    							break
    						}
    					}
    					if ok {
    						break
    					}
    				}
    			}
    		}
    		if !ok {
    			return false
    		}
    	}
    	return true
    }
    

      

  • 相关阅读:
    20155307《网络对抗》网络欺诈技术防范
    20155307《网络对抗》信息搜集与漏洞扫描
    20155307《网络对抗》MSF基础应用
    20155307《网络对抗》恶意代码分析
    20155307《网络对抗》免杀原理与实践
    预习非数值数据的编码方式
    预习原码补码
    C语言ll作业01
    C语言寒假大作战04
    C语言寒假大作战03
  • 原文地址:https://www.cnblogs.com/rsapaper/p/16155587.html
Copyright © 2020-2023  润新知