首页 > 其他 > 详细

[Leetcode]-- Word Search

时间:2014-02-03 13:14:39      阅读:454      评论:0      收藏:0      [点我收藏+]

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.

 

DFS

bubuko.com,布布扣
public class Solution {
    public boolean exist(char[][] board, String word) {
        // Start typing your Java solution below
        // DO NOT write main() function
        if(word.length() == 0)   return true;
        int h = board.length;
        if(h == 0)    return false;
        int w = board[0].length;
        boolean[][] visited = new boolean[h][w];
        
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                if(word.charAt(0) == board[i][j]){
                    if(func(board, word, 0, w, h, j, i, visited)) return true;
                }
            }
        }
        
        return false;
    }
    
    public boolean func(char[][] board, String word, int spos, int w, int h, int x, int y, boolean[][] visited){
        if(spos == word.length())  return true;
        if(x < 0 || x >= w || y < 0 || y >= h)   return false;
        if(visited[y][x] || board[y][x] != word.charAt(spos))   return false;
        
        visited[y][x] = true;
        
        // up
        if(func(board, word, spos + 1, w, h, x, y-1, visited)){
            return true;
        }
        // down
        if(func(board, word, spos + 1, w, h, x, y+1, visited)){
            return true;
        }
        // left
        if(func(board, word, spos + 1, w, h, x-1, y, visited)){
            return true;
        }
        // right
        if(func(board, word, spos + 1, w, h, x+1, y, visited)){
            return true;
        }
        
        visited[y][x] = false;
        
        return false;
    }
}
bubuko.com,布布扣

 

空间优化 HashSet

bubuko.com,布布扣
public class Solution {
    public boolean exist(char[][] board, String word) {
        if(word == null || word.length() < 1)
            return false;
        HashSet<Integer> done = new HashSet<Integer>();
        
        for(int i = 0; i < board.length; i++)
            for(int j =0; j < board[0].length; j++){
                if(generate(board, i, j, 0, word, done))
                    return true;
            }
        
        return false;
    }
    
    private boolean generate(char[][] board, int row, int column, int depth, String word, HashSet<Integer> done){
        if(row < 0 || column < 0 || row >= board.length || column >= board[0].length)
            return false;
        int loc = row * board[0].length + column;
        if(done.contains(loc))
            return false;
        if(board[row][column] == word.charAt(depth)){
            if(depth == word.length()-1)
                return true;
            done.add(loc);
            if(generate(board, row+1, column, depth+1,word,done))
                return true;
            if(generate(board, row, column+1, depth+1,word,done))
                return true;
            if(generate(board, row-1, column, depth+1,word,done))
                return true;
            if(generate(board, row, column-1, depth+1,word,done))
                return true;
            done.remove(loc);
        }
        return false;
    }
}
View Code

 

 

ref: http://www.cnblogs.com/feiling/p/3292649.html

 

网上关于深度优先搜索的总结帖:

http://cuijing.org/interview/leetcode/summary-of-dfs-and-bfs-in-leetcode.html

http://leetcodenotes.wordpress.com/2013/08/24/leetcode-word-search-%E4%BA%8C%E7%BB%B4%E7%9F%A9%E9%98%B5%E6%89%BE%E9%87%8C%E9%9D%A2%E7%9A%84%E5%8D%95%E8%AF%8D/

[Leetcode]-- Word Search

原文:http://www.cnblogs.com/RazerLu/p/3537456.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!