[LeetCode]Word Search --
来源:互联网 发布:苹果手机频谱仪软件 编辑:程序博客网 时间:2024/06/06 19:35
题目:
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.
Given board =
[
[“ABCE”],
[“SFCS”],
[“ADEE”]
]
word = “ABCCED”, -> returns true,
word = “SEE”, -> returns true,
word = “ABCB”, -> returns false.
题目分析:”be constructed from letters of sequentially adjacent cell”应该是说单词可以在它能接触到的邻居字符连续的构成。说的什么鬼,还是举例子吧。board:[“ab”,’cd’] word: “abcd” ->expected false . 因为 在矩阵board中,b、c是不相邻的。
代码实现
#include <iostream>#include<vector>#include<string>using namespace std;/*Problem:Word SearchGiven 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 horizontallyor vertically neighboring. The same letter cell may not be used more than once.*/ bool isFound(vector<vector<char>> &board,const char* w,int i,int j){ int m=board.size();int n=board[0].size(); if(i<0||j<0||i>=m||j>=n||board[i][j]=='\0'||*w!=board[i][j]) return false; if(*(w+1)=='\0')return true; char t=board[i][j]; board[i][j]='\0'; if(isFound(board,w+1,i-1,j)||isFound(board,w+1,i+1,j)||isFound(board,w+1,i,j-1)||isFound(board,w+1,i,j+1)) return true; board[i][j]=t; return false; } bool exist(vector<vector<char>> &board,string word){ int n=board.size(); if(n==0)return false; int m=board[0].size(); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(isFound(board,word.c_str(),i,j)) return true; } } return false; }int main(){ vector<vector<char>> board={{'A','B','C','E'},{'S','F','C','S'},{'A','D','E','E'}}; string word="CCBA"; bool res=exist(board,word); cout << "Hello world!" << res<<endl; return 0;}
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
- 今天开始慢下脚步,开始ios技术知识的查漏补缺。
- left join和inner join举例
- LeanCloud返回新建对象的objectId
- springMVC_后台自动接受map数据
- Android中ScrollView中嵌套ViewPager导致ViewPager不显示的问题
- [LeetCode]Word Search --
- 通过自定义TextView,拉伸每行解决自动换行文字排版参差不齐问题
- 自动调整linux系统时间和时区与Internet时间同步
- Postfit 不能群发邮件的问题
- Mac zend studio 12.0.2中国免费使用办法破解汉化
- ActiveMQ实战
- 针对高版本timpicker/datepicker中ontimeset执行两次的解决办法
- Android shape的属性和使用详解
- EXT.JS初学配置