Word Search [LeetCode]
Problem Description: http://oj.leetcode.com/problems/word-search/
Basic idea: recursively go forward, use char '#' to mark the char visited, so no extra memory is need, don't forget to recover the previous char if the program cannot go forward.
class Solution {
public:
bool existSub(int i, int j, vector<vector<char> > &board, string word){
if(word.size() == )
return true;
if(i < || j < || i >= board.size() || j >= board[].size())
return false;
if(board[i][j] != word[])
return false;
string sub_word = word.substr();
char ch = board[i][j];
board[i][j] = '#';
bool ret = existSub(i - , j, board, sub_word) ||
existSub(i + , j, board, sub_word) ||
existSub(i, j - , board, sub_word) ||
existSub(i, j + , board, sub_word);
if (ret)
return true;
board[i][j] = ch;
return false;
}
bool exist(vector<vector<char> > &board, string word) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(word.size() == )
return true;
for(int i = ; i < board.size(); i++)
for(int j = ; j < board[i].size(); j++)
if(board[i][j] == word[])
if(existSub(i, j, board, word))
return true;
return false;
}
};
Word Search [LeetCode]的更多相关文章
- Word Search leetcode java
题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...
- [LeetCode] Word Search II 词语搜索之二
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- [LeetCode] Word Search 词语搜索
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- 【LeetCode】79. Word Search
Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...
- LeetCode解题报告—— Word Search & Subsets II & Decode Ways
1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...
- Leetcode之回溯法专题-212. 单词搜索 II(Word Search II)
Leetcode之回溯法专题-212. 单词搜索 II(Word Search II) 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单 ...
- Leetcode之回溯法专题-79. 单词搜索(Word Search)
Leetcode之回溯法专题-79. 单词搜索(Word Search) 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元 ...
- [LeetCode] 212. Word Search II 词语搜索之二
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
随机推荐
- mongodb .net driver
1.介绍 The official MongoDB .NET Driver provides asynchronous interaction with MongoDB. Powering the d ...
- Oracle的数据恢复——Flashback用法汇总
/* 11g的flashbackup 分好几种,分别用途不一样. A.flashback database 闪回数据库,简单理解就是把数据库闪回到某个以前的时间点, 能恢复到的最早的SCN, 取决与F ...
- Spring AOP和AspectJ支持
学了Spring之后发现我都不知道java为何物-- 在这一章中有好几节,讲的切面编程 第一节:在项目中启用Spring的AspectJ注解支持 第二节:用AspectJ注解声明aspect 第三节: ...
- Spark ML聚类分析之k-means||
今天更新了电脑上的spark环境,因为上次运行新的流水线的时候,有的一些包在1.6.1中并不支持 只需要更改系统中用户的环境变量即可 然后在eclipse中新建pydev工程,执行环境是python3 ...
- Codeforces Round #287 (Div. 2) C. Guess Your Way Out! 思路
C. Guess Your Way Out! time limit per test 1 second memory limit per test 256 megabytes input standa ...
- phpcms 头部搜索栏上边的 “新闻 | 图片 | 下载 | 专题” 是在哪里修改的?
phpcms 是怎么修改以下 栏目列. 在后台的时候,“模块管理>全站搜索”,可以修改搜索分类
- linux安装phpstorm出现 Startup Error: Unable to detect graphics environment
在centos6.2下在安装phpstorm下遇到 Startup Error: Unable to detect graphics environment 其中jdk安装的版本是 1.7.0_79 ...
- 如何制作exe程序可执行文件
很多软件的运行都需要搭建环境,只有exe文件可以在不安装软件和数据库的环境下运行,那么怎么制作exe程序可执行文件呢,下面天使教你如何制作. 工具/原料 Microsoft Visual St ...
- 【Todo】Python面试题分析记录(修饰器等)
首先,看这一段代码: class A(object): x = 1 gen = (lambda t: (t for _ in xrange(10)))(x) if __name__ == '__mai ...
- count-the-repetitions
https://leetcode.com/problems/count-the-repetitions/ 下面是我的方法,结果对的,超时了... package com.company; class ...