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