LeetCode_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.
AC 代码:
class Solution {
public:
bool search(vector<vector<char> > &board, const string & word, int i, int j, int index, vector<vector<bool>> &flag)
{
if(index == len) return true;
//up
if(i- >= && board[i-][j] == word[index] && flag[i-][j] == false) {
flag[i-][j] = true;
if(search(board, word, i-, j,index+, flag) ) return true;
flag[i-][j] = false;
}
//down
if(i+ < rows && board[i+][j] == word[index] && flag[i+][j] == false){
flag[i+][j] = true;
if(search(board, word, i+, j, index+, flag)) return true;
flag[i+][j] = false;
}
//right
if( j+ < columns && board[i][j+] == word[index] && flag[i][j+] == false){
flag[i][j+] = true;
if(search(board, word, i, j+, index+, flag)) return true;
flag[i][j+] = false;
}
//left
if(j- >= && board[i][j-] == word[index] && flag[i][j-] == false){
flag[i][j-] = true;
if(search(board, word, i, j-, index+, flag)) return true;
flag[i][j-] = false;
}
return false;
}
bool exist(vector<vector<char> > &board, string word) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
rows = board.size();
columns = board[].size();
len = word.size();
if(len == ) return true ;
if(rows * columns < len) return false; for(int i = ; i< rows ;i++)
for(int j = ; j< columns ; j++){
if(board[i][j] == word[])
{
vector<vector<bool>> flag(rows, vector<bool>(columns, false)) ;
flag[i][j] = true;
if(search(board,word, i, j,,flag)) return true;
// flag[i][j] = false;
}
} return false;
}
private:
int rows;
int columns;
int len ;
};
未AC的奇怪代码
class Solution {
public:
bool search(vector<vector<char> > &board, const string & word, int i, int j, int index)
{
if(index == len) return true;
//up
if(i- >= && board[i-][j] == word[index] && flag[i-][j] == false) {
flag[i-][j] = true;
if(search(board, word, i-, j,index+) ) return true;
flag[i-][j] = false;
}
//down
if(i+ < rows && board[i+][j] == word[index] && flag[i+][j] == false){
flag[i+][j] = true;
if(search(board, word, i+, j, index+)) return true;
flag[i+][j] = false;
}
//right
if( j+ < columns && board[i][j+] == word[index] && flag[i][j+] == false){
flag[i][j+] = true;
if(search(board, word, i, j+, index+)) return true;
flag[i][j+] = false;
}
//left
if(j- >= && board[i][j-] == word[index] && flag[i][j-] == false){
flag[i][j-] = true;
if(search(board, word, i, j-, index+)) return true;
flag[i][j-] = false;
}
return false;
}
bool exist(vector<vector<char> > &board, string word) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
rows = board.size();
columns = board[].size();
len = word.size();
if(len == ) return true ;
if(rows * columns < len) return false;
flag.resize(rows, vector<bool>(columns, false)) ;
for(int i = ; i< rows ;i++)
for(int j = ; j< columns ; j++){
if(board[i][j] == word[])
{
flag[i][j] = true;
if(search(board,word, i, j,)) return true;
flag[i][j] = false;
}
} return false;
}
private:
vector<vector<bool>> flag;
int rows;
int columns;
int len ;
};
LeetCode_Word Search的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- 基于WebGL 的3D呈现A* Search Algorithm
http://www.hightopo.com/demo/astar/astar.html 最近搞个游戏遇到最短路径的常规游戏问题,一时起兴基于HT for Web写了个A*算法的WebGL 3D呈现 ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- (转)PHP zval内存回收机制和refcount_gc和is_ref_gc
出处 : http://blog.sina.com.cn/s/blog_75a2f94f0101gygh.html 对于PHP这种需要同时处理多个请求的程序来说,申请和释放内存的时候应该慎之又慎,一不 ...
- 【转】Android 定时器实现的几种方式和removeCallbacks失效问题详解--不错
原文网址:http://blog.csdn.net/xiaanming/article/details/9011193 实现定时器有很多种方式,在这里我简单的介绍几种方式 (1)使用Handler + ...
- Android App Widget的简单使用
App Widget是一些桌面的小插件,比如说天气和某些音乐播放应用,放到桌面去的那部分: 例如: 实现步骤及代码如下: (1)首先,在AndroidManifest.xml中声明一个App Widg ...
- Trie树(字典树) 最热门的前N个搜索关键词
方法介绍 1.1.什么是Trie树 Trie树,即字典树,又称单词查找树或键树,是一种树形结构.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优 ...
- Android项目中gen文件下R文件无法生成的解决的方法
帮一个网友解决R文件无法生成的问题,搜集了些材料特整理例如以下,刚開始学习的人參考他人代码时极易出现此种问题,一般都是xml文件出错,无法被正确解析. gen文件夹无法更新,或者gen文件夹下的R.J ...
- linux高级技巧:heartbeat+lvs(一)
1.heartbeat一个简短的引论: Heartbeat 项目是 Linux-HA project的一个组成部分,它实现了一个高可用集群系统.心跳服务和集群通信是高可用集群的两个关键组 ...
- Qt 5.4正式发布!引入WP,支持HTML5混合开发
北京时间12月11日消息,Digia全资子公司The Qt Company在其 官方博客上宣布,正式发布Qt 5.4,支持HTML5混合开发,引入对于Windows Phone的支持,以及众多跨桌面. ...
- neural style论文解读
相关的代码都在Github上,请参见我的Github,https://github.com/lijingpeng/deep-learning-notes 敬请多多关注哈~~~ 概述 在艺术领域,艺术家 ...
- Angular单页应用&AngularJS内部实现原理
回顾 自定义指令 登录后获取登录信息session 首先在登录验证的时候保存一个user 在学生管理页面中运用ajax调用获取到登录的用户信息 对注销按钮添加点击事件:调用ajax在表现层给user赋 ...
- Activity-在ListFragment中为ListView增加空白视图
有两种方法可以实现为ListView添加空白视图.但是原理都一样: 第一种方法是XML+代码添加: 1.定义emptyView视图 2.调用AdapterView的setEmptyView(empt ...