• 找Bug 用例


    https://leetcode-cn.com/problems/ju-zhen-zhong-de-lu-jing-lcof/

    func exist(board [][]byte, word string) bool {
    	n := len(word)
    	r := len(board)
    	c := len(board[0])
    	if n > r*c {
    		return false
    	}
    	g := func(p, q, a, b int) bool {
    		return (p == a && (q-b == 1 || q-b == -1)) || ((p-a == 1 || p-a == -1) && q == b)
    	}
    	for i := 0; i < n; i++ {
    		v := word[i]
    		p, q, a, b := -1, -1, -1, -1
    		f := false
    		for j := 0; j < r; j++ {
    			for k := 0; k < c; k++ {
    				u := board[j][k]
    				if v == u {
    					p, q = j, k
    					if a != -1 {
    						if !g(p, q, a, b) {
    							return false
    						}
    					}
    					a, b = p, q
    					f = true
    					break
    				}
    			}
    		}
    		if !f {
    			return true
    		}
    	}
    	return true
    }
    

      

    func exist(board [][]byte, word string) bool {
    	n := len(word)
    	r := len(board)
    	c := len(board[0])
    	if n > r*c {
    		return false
    	}
    	adjoin := func(p, q, a, b int) bool {
    		return (p == a && (q-b == 1 || q-b == -1)) || ((p-a == 1 || p-a == -1) && q == b)
    	}
    	m := map[int][]int{}
    	had := func(r, c int) bool {
    		_, e := m[r]
    		if e {
    			for _, v := range m[r] {
    				if v == c {
    					return true
    				}
    			}
    			m[r] = append(m[r], c)
    		} else {
    			m[r] = []int{c}
    		}
    		return false
    	}
    
    	for i := 0; i < n; i++ {
    		v := word[i]
    		p, q, a, b := -1, -1, -1, -1
    		hit := false
    		for j := 0; j < r; j++ {
    			for k := 0; k < c; k++ {
    				u := board[j][k]
    				if v == u {
    					if had(j, k) {
    						continue
    					}
    					p, q = j, k
    					if a != -1 {
    						if !adjoin(p, q, a, b) {
    							return false
    						}
    					}
    					a, b = p, q
    					hit = true
    					break
    				}
    			}
    			if hit {
    				break
    			}
    		}
    		if !hit {
    			return false
    		}
    	}
    	return true
    }
    

      

     边界值BUG

    func exist(board [][]byte, word string) bool {
    	n := len(word)
    	r := len(board)
    	c := len(board[0])
    	if n > r*c {
    		return false
    	}
    	m := map[int][]int{}
    	had := func(r, c int, add bool) bool {
    		_, e := m[r]
    		if e {
    			for _, v := range m[r] {
    				if v == c {
    					return true
    				}
    			}
    			if add {
    				m[r] = append(m[r], c)
    			}
    		} else {
    			if add {
    				m[r] = []int{c}
    
    			}
    		}
    		return false
    	}
    
    	start := [][]int{}
    	v := word[0]
    	for j := 0; j < r; j++ {
    		for k := 0; k < c; k++ {
    			u := board[j][k]
    			if v == u {
    				start = append(start, []int{j, k})
    			}
    		}
    	}
    
    	for _, p := range start {
    		a, b := p[0], p[1]
    		ok := false
    		if b-1 >= 0 {
    			u := board[a][b-1]
    			if !had(a, b-1, false) {
    				if u == v {
    					had(a, b-1, true)
    				}
    			}
    			ok = true
    			continue
    		}
    		if b+1 <= c-1 {
    			u := board[a][b+1]
    			if !had(a, b+1, false) {
    				if u == v {
    					had(a, b+1, true)
    				}
    			}
    			ok = true
    			continue
    		}
    		if a-1 >= 0 {
    			u := board[a-1][b]
    			if !had(a-1, b, false) {
    				if u == v {
    					had(a-1, b, true)
    				}
    			}
    			ok = true
    			continue
    		}
    
    		if a+1 <= r-1 {
    			u := board[a+1][b]
    			if !had(a+1, b, false) {
    				if u == v {
    					had(a+1, b, true)
    				}
    			}
    			ok = true
    			continue
    		}
    		if !ok {
    			return false
    		}
    	}
    
    	return true
    }
    

      

    func exist(board [][]byte, word string) bool {
    	n := len(word)
    	r := len(board)
    	c := len(board[0])
    	if n > r*c {
    		return false
    	}
    
    	m := map[int][]int{}
    	had := func(r, c int, add bool) bool {
    		_, e := m[r]
    		if e {
    			for _, v := range m[r] {
    				if v == c {
    					return true
    				}
    			}
    			if add {
    				m[r] = append(m[r], c)
    			}
    		} else {
    			if add {
    				m[r] = []int{c}
    			}
    		}
    		return false
    	}
    
    	start := [][]int{}
    	v := word[0]
    	for j := 0; j < r; j++ {
    		for k := 0; k < c; k++ {
    			u := board[j][k]
    			if v == u {
    				start = append(start, []int{j, k})
    			}
    		}
    	}
    	if len(start) == 0 {
    		return false
    	}
    	for _, p := range start {
    		a, b := p[0], p[1]
    		ok0 := true
    		for i := 1; i < n; i++ {
    			v := word[i]
    			ok := false
    			if b-1 >= 0 {
    				u := board[a][b-1]
    				if !had(a, b-1, false) {
    					if u == v {
    						had(a, b-1, true)
    						ok = true
    						b--
    						continue
    					}
    				}
    			}
    			if b+1 <= c-1 {
    				u := board[a][b+1]
    				if !had(a, b+1, false) {
    					if u == v {
    						had(a, b+1, true)
    						ok = true
    						b++
    						continue
    					}
    				}
    			}
    			if a-1 >= 0 {
    				u := board[a-1][b]
    				if !had(a-1, b, false) {
    					if u == v {
    						had(a-1, b, true)
    						ok = true
    						a--
    						continue
    					}
    				}
    			}
    			if a+1 <= r-1 {
    				u := board[a+1][b]
    				if !had(a+1, b, false) {
    					if u == v {
    						had(a+1, b, true)
    						ok = true
    						a++
    						continue
    					}
    				}
    			}
    			if !ok {
    				ok0 = false
    				break
    			}
    		}
    		if ok0 {
    			return true
    		}
    	}
    
    	return false
    }
    

      

  • 相关阅读:
    iOS --- UIColor中使用16进制选取颜色
    我的投资、理財、財富观
    CentOS7.1 KVM虚拟化之虚拟机快照(5)
    C++实现简单的内存块自己主动管理
    SQL Server,Access数据库查询易混点和C#中parameter指定参数长度的优缺点
    【转】Android Building System 总结
    【转】Android ROM研究---Android build system增加模块
    【转】Android编译系统详解(三)——编译流程详解
    【转】单独编译android framework模块出现的问题
    【转】Android 驱动开发系列四
  • 原文地址:https://www.cnblogs.com/rsapaper/p/16180875.html
Copyright © 2020-2023  润新知