[LeetCode] Word Search
LeetCode word search
2023-09-11 14:17:25 时间
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 =
[ ["ABCE"], ["SFCS"], ["ADEE"] ]word =
"ABCCED"
, -> returns true
,word =
"SEE"
, -> returns true
,word =
"ABCB"
, -> returns false
.
Array Backtracking
思路一:dfs,但是 Time Limit Exceeded
class Solution { vector<vector<bool> > canUse; public: bool dfs(int dep, int x, int y, vector<vector<char> >& board, string& word) { if(dep == word.size()) return true; int row = board.size(); int col = board[0].size(); if(x < 0 || x >= row || y <0 || y >= col ) return false; if(board[x][y] != word[dep]) return false; //mark canUse[x][y] = false; if(x < row-1 && canUse[x+1][y] ) { if(dfs(dep+1, x+1, y, board, word)) { //mark canUse[x][y] = true; return true; } } if(x >0 && canUse[x-1][y] ) { if(dfs(dep+1, x-1, y, board, word)) { //mark canUse[x][y] = true; return true; } } if(y < col-1 && canUse[x][y+1]) { if(dfs(dep+1, x, y+1, board, word)) { //mark canUse[x][y] = true; return true; } } if(y >0 && canUse[x][y-1]) { if(dfs(dep+1, x, y-1, board, word)) { //mark canUse[x][y] = true; return true; } } canUse[x][y] = true; return false; } bool exist(vector<vector<char> > &board, string word) { int row = board.size(); if(row == 0) return false; int col = board[0].size(); if(word.size() > row*col) return false; canUse.clear(); vector<bool> tmp(col, true); canUse.resize(row, tmp); for(int i = 0; i< row; i++) { for(int j = 0; j< col; j++) { if(dfs(0, i, j, board, word )) return true; } } return false; } };
参考了https://github.com/soulmachine/leetcode 感觉也差不多,貌似少了一些边界条件判断,就AC了
不同之处用红色标注了,且x y 的越界也不需要判断了。。
class Solution { vector<vector<bool> > canUse; public: bool dfs(int dep, int x, int y, vector<vector<char> >& board, string& word) { if(dep == word.size()) return true; int row = board.size(); int col = board[0].size(); if(x < 0 || x >= row || y <0 || y >= col ) return false; if(canUse[x][y] == false) return false; if(board[x][y] != word[dep]) return false; //mark canUse[x][y] = false; bool retVal = dfs(dep+1, x+1, y, board, word) || dfs(dep+1, x-1, y, board, word) || dfs(dep+1, x, y+1, board, word) || dfs(dep+1, x, y-1, board, word); //mark canUse[x][y] = true; return retVal; } bool exist(vector<vector<char> > &board, string word) { int row = board.size(); if(row == 0) return false; int col = board[0].size(); canUse.clear(); vector<bool> tmp(col, true); canUse.resize(row, tmp); for(int i = 0; i< row; i++) { for(int j = 0; j< col; j++) { if(dfs(0, i, j, board, word )) return true; } } return false; } };
相关文章
- Leetcode: Sum of Two Integers && Summary: Bit Manipulation
- Leetcode: First Bad Version
- Leetcode: Word Search II
- Leetcode: Length of Last Word
- Word控件Spire.Doc 【页眉页脚】教程(10): 锁定标题以防止在 C# 中编辑 word 文档
- Word控件Spire.Doc 【Table】教程(14): 如何在C#中为word表格设置AutoFit选项
- Word控件Spire.Doc 【页面背景】教程(7) ;在 C# 中为 word 文档设置图像背景
- Word控件Spire.Doc 【图像形状】教程(10): 如何在C#中重置word文档的形状大小
- Word控件Spire.Doc 【图像形状】教程(9): 如何在 C# 和 VB.NET 中从 word 文档中删除形状
- Word控件Spire.Doc 转换教程(二十二): 将word文档中的隐藏文本保存为PDF
- Word控件Spire.Doc 转换教程(十三):在word文档和HTML中嵌入图像支持
- Word控件Spire.Doc 【Table】教程(13): 如何在 C# 中向现有的 word 表添加一行
- leetcode 62. 不同路径-动态规划及优化,双100%
- SpringBoot+Poi-tl根据Word模板动态生成word(含动态行表格、合并单元格)
- SpringBoot导出Word方式一:根据Word模板动态生成word(Poi-tl)
- 【LeetCode】174. Dungeon Game
- 【LeetCode】58. Length of Last Word
- [LeetCode] 1333. Filter Restaurants by Vegan-Friendly, Price and Distance 餐厅过滤器
- [LeetCode] 1314. Matrix Block Sum 矩阵区域和
- [LeetCode] Detect Capital 检测大写格式
- [LeetCode] 411. Minimum Unique Word Abbreviation 最短的独一无二的单词缩写
- [LeetCode] Word Break II 拆分词句之二
- [LeetCode] 58. Length of Last Word 求末尾单词的长度