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 ...
随机推荐
- Cheatsheet: 2015 01.01~ 01.31
JAVA JVM Architecture Improving Lock Performance in Java 10 Best Java Tools That Every Java Programm ...
- Cheatsheet: 2014 10.01 ~ 10.30
.NET ASP.NET Web Api: Unwrapping HTTP Error Results and Model State Dictionaries Client-Side HTTP 20 ...
- Java_JDK_TreeMap
(一)TreeMap TreeMap使用的是红黑树来实现的,所以重点是红黑树的插入和删除. 红黑树的3个特性: 根节点和所有外部节点的颜色都是黑色的: 从根节点到外部节点的途中没有连续两个节点的颜色是 ...
- 如何设计点击点击一个div,其他div做出对应反应,以及获取一个节点下的子节点
<div id="show"> <div>1</div> <div>2</div> <div>3</d ...
- [原创] 使用LP Wizard 10.5 制作 Allegro PCB封装
本文只讲述使用 Calculator 和 Wizard 功能制作封装,通常学会使用这种方法,通用的标准封装就都可以生成了.下面以一个简单的SOIC-8封装的芯片来说明软件使用方法. 第一步,查找相关d ...
- [SAP ABAP开发技术总结]屏幕跳转
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- 学习Berkeley DB- 入门
1 导言 首先,我们要了解Berkeley DB的一些基本特性,在IBM的开发网站上有篇文章对其有比较清晰的介绍: 这篇文章讲到了BDB的设计思想和核心数据结构.以及数据访问算法:并有常用函数使用范例 ...
- 解决使用jQuery采用append添加的元素事件无效的方法
<html> <head> <script type="text/javascript" src="/jquery/jquery.js&qu ...
- iOS - UIAlertView
前言 NS_CLASS_DEPRECATED_IOS(2_0, 9_0, "UIAlertView is deprecated. Use UIAlertController with a p ...
- CentOS6下yum下载的包存放路径
http://showerlee.blog.51cto.com/2047005/1169818 yum下载下来的文件保存默认路径是: /var/cache/yum 修改yum配置文件 /etc/yum ...