123. 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.
Example
Given board =12345[ "ABCE", "SFCS", "ADEE"]
word = “ABCCED”, -> returns true,
word = “SEE”, -> returns true,
word = “ABCB”, -> returns false.
DFS
有个点需要注意一下,当找到之后,就要停止DFS,所以要检查flag
以下代码是自己写的,也可以参考九章: http://www.jiuzhang.com/solutions/word-search/
|
|