LintCode 123 · Word Search (DFS字符处理经典题!)

news/2024/10/17 15:29:35/

123 · Word Search
Algorithms
Medium
Description
Given a 2D board and a string word, find if the string word exists in the grid.

The string 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.

The dimension of the letter matrix does not exceed 100, and the length of the string does not exceed 100.

Example
Example 1:

Input:

board = [“ABCE”,“SFCS”,“ADEE”]
word = “ABCCED”
Output:

true
Explanation:

[
A B C E
S F C S
A D E E
]
(0,0)->(0,1)->(0,2)->(1,2)->(2,2)->(2,1)

Example 1:

Input:

board = [“z”]
word = “z”
Output:

true
Explanation:

[ z ]
(0,0)

解法1:DFS+hashset。
很多小地方需要注意,特别是visited[][]数组什么时候clear掉。

class Solution {
public:/*** @param board: A list of lists of character* @param word: A string* @return: A boolean*/bool exist(vector<vector<char>> &board, string &word) {int m = board.size();if (m == 0) return word.empty();int n = board[0].size();string sol = "";for (int i = 0; i < word.size(); i++) {string tmpStr = word.substr(0, i + 1);s.insert(tmpStr);}vector<vector<bool>> visited;for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {sol.clear(); //这里每次都要clear!不然后面的操作就append到s上面!visited.resize(m, vector<bool>(n, false));sol += board[i][j];if (check(board, word, sol, visited, i, j)) return true;}}return false;}
private:int dx[4] = {0, 0, 1, -1};int dy[4] = {1, -1, 0, 0};set<string> s;bool check(vector<vector<char>> &board, string &word, string sol, vector<vector<bool>> &visited, int x, int y) {if (s.find(sol) == s.end()) return false;if (sol == word) {return true;}if (sol.size() >= word.size()) return false;visited[x][y] = true;for (int i = 0; i < 4; i++) {int newX = x + dx[i];int newY = y + dy[i];if (newX >= 0 && newX < board.size() && newY >= 0 && newY < board[0].size() && !visited[newX][newY]) {sol += board[newX][newY];if (check(board, word, sol, visited, newX, newY)) return true;else sol.pop_back();  //这一行重要!因为sol还要被for循环的其它i用到!}}visited[x][y] = false; //这一行重要,只有当dfs能够一直往下进行,visited[][]才不用动,否则如果dfs中途退出,visited[][]要clear。不然就跟后面操作冲突!return false; //这里别忘了,不然默认可能会返回true!!!}
};

二刷:不需要set。每次比较当前位置的字符就可以了。

class Solution {
public:/*** @param board: A list of lists of character* @param word: A string* @return: A boolean*/bool exist(vector<vector<char>> &board, string &word) {int m = board.size();if (m == 0) return word.empty();int n = board[0].size();string sol = "";vector<vector<bool>> visited;for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {sol.clear();visited.resize(m, vector<bool>(n, false));sol += board[i][j];if (check(board, word, sol, 0, visited, i, j)) return true;}}return false;}
private:int dx[4] = {0, 0, 1, -1};int dy[4] = {1, -1, 0, 0};bool check(vector<vector<char>> &board, string &word, string sol, int pos, vector<vector<bool>> &visited, int x, int y) {if (sol[pos] != word[pos]) return false;if (sol == word) {return true;}//if (sol.size() >= word.size()) return false; //这一行不需要!如果不匹配早就返回了,匹配上面也会返回true。visited[x][y] = true;for (int i = 0; i < 4; i++) {int newX = x + dx[i];int newY = y + dy[i];if (newX >= 0 && newX < board.size() && newY >= 0 && newY < board[0].size() && !visited[newX][newY]) {sol += board[newX][newY];if (check(board, word, sol, pos + 1, visited, newX, newY)) return true;else sol.pop_back();  //这一行重要!因为sol还要被for循环的其它i用到!}}visited[x][y] = false; //这一行重要,只有当dfs能够一直往下进行,visited[][]才不用动,否则如果dfs中途退出,visited[][]要clear。不然就跟后面操作冲突!return false;}
};

三刷: DFS+Trie
TBD

四刷:BFS
TBD


http://www.ppmy.cn/news/1266288.html

相关文章

加索引后 sql loader-951

加索引后 sql loader-951 现象解决过程最终解决 现象 之前使用sqlldr正常&#xff0c;加表索引后使用sqlldr时 报错 SQL Loader-951 解决过程 百度&#xff0c;说可能是锁表&#xff08;或者表未提交&#xff09; 查看没有对应未commit数据&#xff0c;且没有锁表。查看对应…

SQL语句---带NOT IN关键字的条件查询

介绍 使用not in关键字进行条件查询。 命令 select 字段1,字段2 from 表名 where 字段名 not in (元素1,元素2);例子 查询a表中id不等于1和2的数据&#xff1a; select * from a where id not in (1,2);

Linux——web网站服务(一)

一、安装httpd服务器Apache网站服务 1、准备工作 为了避免发送端口冲突&#xff0c;程序冲突等现象&#xff0c;卸载使用rpm方式安装的httpd #使用命令检查是否下载了httpd [rootserver ~]# rpm -qa httpd #如果有则使用 [rootserver ~]# rpm -e httpd --nodeps Apache的配置…

uni-app 微信小程序之好看的ui登录页面(四)

文章目录 1. 页面效果2. 页面样式代码 更多登录ui页面 uni-app 微信小程序之好看的ui登录页面&#xff08;一&#xff09; uni-app 微信小程序之好看的ui登录页面&#xff08;二&#xff09; uni-app 微信小程序之好看的ui登录页面&#xff08;三&#xff09; uni-app 微信小程…

优势怪代码

#include <stdio.h> int main() { int t; scanf("%d", &t); // 读取测试用例的数量 while (t--) { // 对每个测试用例进行处理 int n, max1 0, max2 0, k 0; scanf("%d", &n); // 读取数组的大小 in…

leetcode206. 反转链表

题目描述 给你单链表的头节点 head &#xff0c;请你反转链表&#xff0c;并返回反转后的链表。 示例 1&#xff1a; 输入&#xff1a;head [1,2,3,4,5] 输出&#xff1a;[5,4,3,2,1]示例 2&#xff1a; 输入&#xff1a;head [1,2] 输出&#xff1a;[2,1]示例 3&#xf…

AVP对纵向控制ESP(Ibooster)的需求规范

目录 1. 版本记录... 3 2. 文档范围和控制... 4 2.1 目的/范围... 4 2.2 文档冲突... 4 2.3 文档授权... 4 2.4 文档更改控制... 4 3. 功能概述... 5 4. 系统架构... 6 5. 主要安全目标... 7 5.1 …

Spring Boot 常用注解分类

目录 1.核心注解&#xff1a;2.配置相关注解&#xff1a;3.控制器相关注解&#xff1a;4.数据访问相关注解&#xff1a;5.测试相关注解&#xff1a;6.条件注解&#xff1a;7.AOP相关注解&#xff1a;8.定时任务相关注解&#xff1a;9.消息队列相关注解&#xff1a;10.Spring Se…