找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
}

  

 

posted @ 2022-04-17 13:47  papering  阅读(35)  评论(0编辑  收藏  举报