leetcode70word-search
题目描述
[↵ ["ABCE"],↵ ["SFCS"],↵ ["ADEE"]↵]
单词 ="ABCCED", -> 返回 true,
单词 ="SEE", ->返回 true,
单词 ="ABCB", -> 返回 false.
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", -> returnstrue,
word ="SEE", -> returnstrue,
class Solution {
public:
bool isOut(int r,int c,int rows,int cols){
return c<0 || c>=cols || r<0 || r>=rows;
}
bool DFS(vector< vector< char >>&board,int r, int c,string &word,int start){
if (start>=word.size())
return true;
if (isOut(r,c,board.size(),board[0].size() )|| word[start]!=board[r][c])
return false;
int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
char tmp=board[r][c];
board [r][c]='.';
for (int i=0;i<4;++i){
if (DFS(board,r+dx[i],c+dy[i],word,start+1))
return true;
}
board[r][c]=tmp;
return false;
}
bool exist(vector<vector<char> > &board, string word) {
int rows=board.size(),cols=board[0].size();
for (int r=0;r<rows;++r)
for (int c=0;c<cols;++c){
if (board[r][c]==word[0])
if (DFS(board,r,c,word,0))
return true;
}
return false;
}
};
leetcode70word-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 ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
随机推荐
- xshell的下载与使用
昨天刚刚立下每天一篇原创的宏图,今天就停电,到11:05才来电,没办法,学习也学不了了,就只有发一下学过的东西,才能维持得了立下的flag的那个样子,而且,老铁们,今天就不写什么原创博客了,今天转载, ...
- wine实用经验教程
本篇讲类unix系统下的用以模拟运行Windows程序的wine.会从普通使用者的比较实用的角度去讲.有专为国内用户准备的内容. 本篇面向有Linux经验但对wine不熟悉的人. wine可靠吗?该不 ...
- logstash-配置文件详解
kafka 将 kafka topic 中的数据读取为事件 kafka{ bootstrap_servers=> "kafka01:9092,kafka02:9092,kaf ...
- kinaba 安装踩坑: FATAL Error: [elasticsearch.url]: definition for this key is missing 转
安装 https://www.jianshu.com/p/875457cb8da6 操作系统:Linux kibana 版本: 7.4.0 1. 在/etc/yum.repos.d/ 下新建 ...
- 【事件中心 Azure Event Hub】Event Hub Java SDK的消费端出现不消费某一个分区中数据的情况,出现IdleTimerExpired错误消息记录
问题情形 使用Java SDK编写的Event Hub消费端应用,随机性遇见了某个分区没有消费消息的情况,在检查日志时候,有发现IdelTimeExpired的错误记录.在重启应用后,连接EventH ...
- Pytest之使用断言指定异常
官网的翻译是使用断言抛出指定异常,当我觉得他这里更应该指的是 Pytest 断言错误类型# 使用raise在测试方法中指定异常的类型,这点和java还是蛮像的呢,具体示例如下: import pyte ...
- C# 将dataset数据导出到excel中
//添加引用 NPOI.dll //添加 using NPOI.HSSF.UserModel; /// <summary> /// 导出数据到Excel /// </summary& ...
- 联赛模拟测试22 D. 简单计算
题目描述 分析 \(\sum_{i=0}^p[(p|qi)?0:1]=\sum_{i=0}^p[(p/gcd(p,q)|qi/gcd(p,q))?0:1]=\sum_{i=0}^p[(p/gcd(p, ...
- 在WPF中一种较好的绑定Enums数据方法
引言 在你使用wpf应用程序开发的时候,是否需要进行数据绑定到Enum数据呢?在这篇文章中,我将向你展示在WPF中处理Enum数据绑定的方法. 假设存在一个这样的Enum数据的定义,具体内容如下文代码 ...
- Redis的一些问题
date: 2020-10-15 10:58:00 updated: 2020-10-19 18:00:00 Redis的一些问题 Remote Dictionary Server 底层C写的 类似于 ...