在矩阵中查找字符串 Word Search
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
問題:
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?=
word?=?"ABCCED", -> returns?true,
word?=?"SEE", -> returns?true,
word?=?"ABCB", -> returns?false.
解決:
【題意】題目要求在字母矩陣中,可以上下左右移動,看是否可以得到給定的字符串,同一位置的字符串只能使用一次。
① 使用dfs遍歷。向上下左右四個方向移動。
class Solution{//10ms
? ? public boolean exist(char[][] board, String word) {
? ? ? ? int m = board.length;
? ? ? ? int n = board[0].length;
? ? ? ? boolean res = false;
? ? ? ? for(int i = 0; i < m; i ++){
? ? ? ? ? ? for(int j = 0; j < n; j ++){
? ? ? ? ? ? ? ?if(dfs(board,word,i,j,0)){
? ? ? ? ? ? ? ? ? ?res = true;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? }
? ? ? ? } ? ?
? ? ? ? return res;
? ? } ? ??
? ? public boolean dfs(char[][] board, String word, int i, int j, int k){
? ? ? ? int m = board.length;
? ? ? ? int n = board[0].length;
? ? ? ? if(i < 0 || j < 0 || i >= m || j >= n){
? ? ? ? ? ? return false;
? ? ? ? } ?
? ? ? ? if(board[i][j] == word.charAt(k)){
? ? ? ? ? ? char tmp = board[i][j];//記錄當(dāng)前位置
? ? ? ? ? ? board[i][j] = '#';//該位置已經(jīng)被使用
? ? ? ? ? ? if(k == word.length() - 1){//匹配完畢
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }else if(dfs(board, word, i - 1, j, k + 1)
? ? ? ? ? ? || dfs(board, word, i + 1, j, k + 1)
? ? ? ? ? ? || dfs(board, word, i, j - 1, k + 1)
? ? ? ? ? ? || dfs(board, word, i, j + 1, k + 1)){//遍歷上下左右四個方向
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }
? ? ? ? ? ? board[i][j] = tmp;//還原
? ? ? ? } ? ??
? ? ? ? return false;
? ? }
}
class Solution {//14ms
? ? public boolean exist(char[][] board, String word) {
? ? ? ? for(int i = 0; i < board.length; i ++)
? ? ? ? ? ? for(int j = 0; j < board[0].length; j ++){
? ? ? ? ? ? ? ? if(dfs(board, word, i, j, 0))
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }
? ? ? ? return false;
? ? }
? ? private boolean dfs(char[][] board, String word, int i, int j, int k){
? ? ? ? if(k == word.length()) return true;
? ? ? ? if(i > board.length-1 || i <0 || j<0 || j >board[0].length - 1 || board[i][j] != word.charAt(k))
? ? ? ? ? ? return false;
? ? ? ? board[i][j] = '#';//改變該位置的值
? ? ? ? boolean flag = ? ?dfs(board, word, i - 1, j, k + 1) ||
? ? ? ? ? ? ? ? ? ? ? ? ? ? dfs(board, word, i, j - 1, k + 1) ||
? ? ? ? ? ? ? ? ? ? ? ? ? ? dfs(board, word, i, j + 1, k + 1) ||
? ? ? ? ? ? ? ? ? ? ? ? ? ? dfs(board, word, i + 1, j, k + 1);//遍歷上下左右四個方向
? ? ? ? board[i][j] = word.charAt(k);//還原
? ? ? ? return flag;
? ? }
}
② 在discuss中看到,對處理已遍歷的字符的處理方法不同。
class Solution {//9ms
????public boolean exist(char[][] board, String word) {
????????int n = board.length, m = board[0].length;
????????if (word.length() > n * m) ?return false;
????????for (int i = 0; i < n; i++){
????????????for (int j = 0; j < m; j++){
????????????????if (dfs(board, word, i, j, 0))
????????????????????return true;
????????????}
????????}
????????return false;
????}
????private boolean dfs(char[][] board, String word, int i, int j, int k){
????????if (k == word.length()) return true;
????????if (i < 0 || i == board.length || j < 0 || j == board[0].length)
????????????return false;
????????if (board[i][j] != word.charAt(k)) ?return false;
????????board[i][j] ^= 256;//改變該位置的值,將其變?yōu)榉亲?/span>母
????????boolean flag = dfs(board, word, i - 1, j, k + 1) || dfs(board, word, i + 1, j, k + 1)?
????????????????????|| dfs(board, word, i, j - 1, k + 1) || dfs(board, word, i, j + 1, k + 1);
????????board[i][j] ^= 256; //還原該位置的值
????????return flag;
????}
}
轉(zhuǎn)載于:https://my.oschina.net/liyurong/blog/1538613
總結(jié)
以上是生活随笔為你收集整理的在矩阵中查找字符串 Word Search的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 给定数组的子集 Subsets
- 下一篇: 你的Java代码是否符合规范,来测测便知