79. Word Search(回溯法)

Given a 2D board and a word, find if the word exists in the grid.

The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.

For example,

Given board =

[

['A','B','C','E'],

['S','F','C','S'],

['A','D','E','E']

]

word = "ABCCED", -> returns true,

word = "SEE", -> returns true,

word = "ABCB", -> returns false.

在数组里找单词,要求在数组里找到的字母都是连着的,并且每个字母只能用一次。

代码:


79. Word Search(回溯法)_第1张图片
参考代码


解法分析:利用的是回溯法。对 board[i][j] 位置的字符进行上下左右的比较。


参考leetcode

你可能感兴趣的:(79. Word Search(回溯法))