zoukankan      html  css  js  c++  java
  • Word Search

    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.

    Analyse: This problem has high time complexity. For each element in the 2D board, do the matching in four directions. If the length of searched elements equals to the string length, return true. If out of board index or the item doesn't equal to the corresponding string[i], return false. 

    Runtime: 92ms

     1 class Solution {
     2 public:
     3     bool exist(vector<vector<char>>& board, string word) {
     4         if(board.empty() || board[0].empty()) return false;
     5         
     6         vector<vector<bool> > visited(board.size(), vector<bool>(board[0].size(), false));
     7         for(int i = 0; i < board.size(); i++){
     8             for(int j = 0; j < board[i].size(); j++){
     9                 if(dfs(board, word, visited, i, j, 0)) return true;
    10             }
    11         }
    12         return false;
    13     }
    14     bool dfs(vector<vector<char> >& board, string word, vector<vector<bool> >& visited, int row, int col, int index){
    15         if(index == word.length()) return true;
    16         
    17         if(row < 0 || col < 0 || row >= board.size() || col >= board[0].size() || visited[row][col] || word[index] != board[row][col])
    18             return false;
    19         
    20         visited[row][col] = true;
    21         if(dfs(board, word, visited, row + 1, col, index + 1)) return true;
    22         if(dfs(board, word, visited, row - 1, col, index + 1)) return true;
    23         if(dfs(board, word, visited, row, col + 1, index + 1)) return true;
    24         if(dfs(board, word, visited, row, col - 1, index + 1)) return true;
    25         visited[row][col] = false;
    26         return false;
    27     }
    28 };

    Runtime: 382ms

    class Solution {
    public:
        bool search(vector<vector<char> >& board, string word, int i, int j, int depth, vector<vector<bool> >& used){
            if(i >= board.size() || i < 0 || j >= board[i].size() || j < 0 || board[i][j] != word[depth] || used[i][j])
                return false;
            used[i][j] = true;
            if(depth == word.size() - 1) return true;
            if(search(board, word, i - 1, j, depth + 1, used)  ||
                search(board, word, i + 1, j, depth + 1, used) ||
                search(board, word, i, j - 1, depth + 1, used) ||
                search(board, word, i, j + 1, depth + 1, used))
                return true;
            used[i][j] = false;
            return false;
        }
        bool exist(vector<vector<char>>& board, string word) {
            if(board.empty() || board[0].empty() || word.empty()) return false;
        
            for(int i = 0; i < board.size(); i++){
                for(int j = 0; j < board[i].size(); j++){
                    if(board[i][j] == word[0]){
                        vector<vector<bool > > used(board.size(), vector<bool>(board[0].size(), false));
                        if(search(board, word, i, j, 0, used))
                            return true;
                    }
                }
            }
            return false;
        }
    };
  • 相关阅读:
    PHP使用file_put_contents写入文件的优点
    CSS3如何去除 inline block 元素之间多出的空格
    PHP中根据IP地址判断所在城市等信息
    CSS3使用Animation为同一个元素添加多个动画效果
    Ubuntu Server 12.04 安装 Jabberd2 服务器
    Mac系统使用命令行快捷打开Sublime
    在 Flash ActionScript 2.0 中调用 Javascript 方法
    elem.attr()无法正确判断checkbox是否选中
    android 竖向viewpager
    【iOS入门】UITableView
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/4776980.html
Copyright © 2011-2022 走看看