Word Search - leetcode

来源:互联网 发布:alive软件 编辑:程序博客网 时间:2024/06/08 15:14

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.

自己写了一个,我觉得我大体思路是对了,但是超时了。这里特别要注意的是,设置一个used数组方便我们观察哪个点被访问过了。当访问一个点而没有继续向下走时,最后要吧used设置回未访问的状态:

public class Solution { public boolean exist(char[][] board, String word) { if(board == null) return false; else if(word == null) return true; else if(word.length() == 0) return true; else if(board.length == 0 || board[0].length == 0) return false;        for(int i = 0; i < board.length; i ++){        for(int j = 0; j < board[0].length; j ++){        int[][] used = new int[board.length][board[0].length];        if(trySearch(board, word, i, j, used)){        return true;        }        }        }return false;     }        public boolean trySearch(char[][] board, String word, int row, int col, int[][] used){if(used[row][col] == 1)return false;if(board[row][col] != word.charAt(0))return false;if(word.length() == 1)return true;used[row][col] = 1;if(row - 1 >= 0){boolean up = trySearch(board, word.substring(1), row - 1, col, used);if(up)return true;}if(row + 1 < board.length){boolean down = trySearch(board, word.substring(1), row + 1, col, used);if(down)return true;}if(col - 1 >= 0){boolean left = trySearch(board, word.substring(1), row , col - 1, used);if(left)return true;}if(col + 1 < board[0].length){boolean right = trySearch(board, word.substring(1), row , col + 1, used);if(right)return true;}used[row][col] = 0;return false;}}


读了一些代码,做了一个小修改就过了,太神奇了:

就是在for loop外设置了数组used,而不是每次for都new一个新used数组。

public class Solution { public boolean exist(char[][] board, String word) { if(board == null) return false; else if(word == null) return true; else if(word.length() == 0) return true; else if(board.length == 0 || board[0].length == 0) return false; int [][] used = new int[board.length][board[0].length];        for(int i = 0; i < board.length; i ++){        for(int j = 0; j < board[0].length; j ++){        if(trySearch(board, word, i, j, used)){        return true;        }        }        }return false;     }        public boolean trySearch(char[][] board, String word, int row, int col, int[][] used){if(used[row][col] == 1)return false;if(board[row][col] != word.charAt(0))return false;if(word.length() == 1)return true;used[row][col] = 1;if(row - 1 >= 0){boolean up = trySearch(board, word.substring(1), row - 1, col, used);if(up)return true;}if(row + 1 < board.length){boolean down = trySearch(board, word.substring(1), row + 1, col, used);if(down)return true;}if(col - 1 >= 0){boolean left = trySearch(board, word.substring(1), row , col - 1, used);if(left)return true;}if(col + 1 < board[0].length){boolean right = trySearch(board, word.substring(1), row , col + 1, used);if(right)return true;}used[row][col] = 0;return false;}}



0 0
原创粉丝点击