LeetCode: Word Search
来源:互联网 发布:网络聊天室开发成本 编辑:程序博客网 时间:2024/06/06 03:34
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
.class Solution {public: bool exist(vector<vector<char> > &board, string word) { if(word.size() == 0) return true; for(int i = 0; i < board.size(); i++) for(int j = 0; j < board[i].size(); j++) { if(dfs(0, board, word, i, j)) return true; } return false; } bool dfs(int index, vector<vector<char> > &board, string word, int x, int y) { if(board[x][y] != word[index]) return false; else { if(index == word.size() - 1) return true; char store = board[x][y]; if(x-1 >= 0) { board[x][y] = '#'; if(dfs(index+1, board, word, x-1, y)) return true; board[x][y] = store; } if(x+1 < board.size()) { board[x][y] = '#'; if(dfs(index+1, board, word, x+1, y)) return true; board[x][y] = store; } if(y-1 >= 0) { board[x][y] = '#'; if(dfs(index+1, board, word, x, y-1)) return true; board[x][y] = store; } if(y+1 < board[0].size()) { board[x][y] = '#'; if(dfs(index+1, board, word, x, y+1)) return true; board[x][y] = store; } return false; } }};
Round 2:
class Solution {public: bool exist(vector<vector<char> > &board, string word) { if(board.size() == 0) return false; int sizeX = board.size(); int sizeY = board[0].size(); for(int i = 0; i < sizeX; i++) for(int j = 0; j < sizeY; j++) { if(dfs(i, j, word, board, 0)) return true; } return false; }private: bool dfs(int x, int y, string target, vector<vector<char> > &board, int index) { if(board[x][y] != target[index]) return false; if(index == target.size()-1) return true; if(x-1 >= 0 && board[x-1][y] == target[index+1]) { char temp = board[x][y]; board[x][y] = '#'; if(dfs(x-1, y, target, board, index+1)) { return true; } board[x][y] = temp; } if(y-1 >= 0 && board[x][y-1] == target[index+1]) { char temp = board[x][y]; board[x][y] = '#'; if(dfs(x, y-1, target, board, index+1)) { return true; } board[x][y] = temp; } if(y+1 < board[0].size() && board[x][y+1] == target[index+1]) { char temp = board[x][y]; board[x][y] = '#'; if(dfs(x, y+1, target, board, index+1)) { return true; } board[x][y] = temp; } if(x+1 < board.size() && board[x+1][y] == target[index+1]) { char temp = board[x][y]; board[x][y] = '#'; if(dfs(x+1, y, target, board, index+1)) { return true; } board[x][y] = temp; } return false; }};
0 0
- LeetCode: Word Search
- [Leetcode] Word Search
- LeetCode : Word Search
- Leetcode: Word Search
- [LeetCode] Word Search
- leetcode 56: Word Search
- Leetcode 79 Word Search
- [leetcode ] word search
- [Leetcode] Word Search
- [LeetCode]Word Search
- Leetcode Word Search
- [leetcode]word search
- [leetcode] Word Search
- LeetCode-Word Search
- [LeetCode] Word Search
- LeetCode - Word Search
- leetcode word search
- LeetCode:Word Search
- 使用google authenticator为你提供一次一密的ssh登录(by quqi99)
- 使用JFreeChart实现基于Web的柱状图
- JsonModel的使用
- 第十周项目二 2
- 关于上位机与arm使用自定义协议通过串口传输文件时遇到的疑难问题的解决
- LeetCode: Word Search
- setTimeStamp()与setDate()区别
- SICP 1.21 1.22 体会
- OgreMax安装&使用
- Xcode无证书打ipa包
- 病毒木马查杀实战第002篇:熊猫烧香之手动查杀
- 日语学习之新版初级标准日本语-14 デパートへいって買い物しました
- Redis入门经典——The Little Redis Book (翻译)
- iphone ipad 开发应用内实现多语言自由切换