【LeetCode】212. Word Search II 解题报告(C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/word-search-ii/
题目描述
Given a 2D board and a list of words from the dictionary, find all words in the board.
Each word must 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 in a word.
Example:
Input:
words = ["oath","pea","eat","rain"] and board =
[
['o','a','a','n'],
['e','t','a','e'],
['i','h','k','r'],
['i','f','l','v']
]
Output: ["eat","oath"]
Note:
1.You may assume that all inputs are consist of lowercase letters a-z.
题目大意
给定一组坐标,找出四个顶点使其能构成长方形,求最小的长方形的面积。注意,边有可能不和x,y轴平行。
解题方法
前缀树
这个题仍然是前缀树的题目,但是我抠了很久。。果然Hard题就是不好写啊。
首先,这个题给出的words特别多,但是board的大小反而稍微小了一点,但是题目没有提示,这就造成了在board中搜索每个单词的方法会超时。正确的做法应该是,直接对board进行搜索,判断搜索过程中能不能构成words中的某个字符串。
如果我们保存路径,再去word中查,这个效率就很低了,这里对前缀树进行了改变,对于单词节点不去保存isWord,而是保存现在位置的字符串是什么,那么在board搜索过程中,如果恰好找到了一个前缀树中的单词,那就放到结果里。
这个题我一直在错,却想不明白的地方是在找到一个单词之后对p->str进行了清空的同时,return了!这是错误的!因为对于相同前缀的字符串,我们还要继续向后搜索的。比如"anes","anesis"如果在第一个单词搜索到之后return,就不可能搜索到第二个单词。所以不能return.
另外,返回的结果排不排序不影响,对时间影响不大。
当代码比较长的时候,一定要保证写出的每个模块是对的,只有这样才能减少检查的时间。特别是细节错误,千万不能犯。
C++代码如下:
class TrieNode {
public:
vector<TrieNode*> child;
string str;
TrieNode() : child(26, nullptr), str("") {};
~TrieNode() {
for (auto c : child) delete c;
}
};
class Trie {
public:
TrieNode* root;
Trie() : root(new TrieNode()){};
void insert(string word) {
TrieNode* p = root;
for (char c : word) {
int i = c - 'a';
if (!p->child[i])
p->child[i] = new TrieNode();
p = p->child[i];
}
p->str = word;
}
};
class Solution {
public:
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
const int M = board.size(), N = board[0].size();
vector<vector<bool>> visited(M, vector<bool>(N, false));
Trie trie;
for (string word : words)
trie.insert(word);
vector<string> res;
for (int r = 0; r < M; r ++) {
for (int c = 0; c < N; c++) {
if (trie.root->child[board[r][c] - 'a']) {
helper(board, trie.root->child[board[r][c] - 'a'], r, c, visited, res);
}
}
}
sort(res.begin(), res.end());
return res;
}
void helper(vector<vector<char>>& board, TrieNode* p, int r, int c, vector<vector<bool>>& visited, vector<string>& res) {
const int M = board.size(), N = board[0].size();
if (!p->str.empty()){
res.push_back(p->str);
p->str.clear();
}
visited[r][c] = true;
for (auto d : dirs) {
int nx = r + d.first;
int ny = c + d.second;
if (nx < 0 || nx >= M || ny < 0 || ny >= N || visited[nx][ny] || !p->child[board[nx][ny] - 'a'])
continue;
helper(board, p->child[board[nx][ny] - 'a'], nx, ny, visited, res);
}
visited[r][c] = false;
}
private:
vector<pair<int, int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
};
日期
2018 年 12 月 23 日 —— 周赛成绩新高
【LeetCode】212. Word Search II 解题报告(C++)的更多相关文章
- 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] 212. Word Search II 词语搜索 II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- [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#212]Word Search II
Problem: Given a 2D board and a list of words from the dictionary, find all words in the board. Each ...
- leetcode 79. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
- LeetCode: Word Break II 解题报告
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- 【leetcode】212. Word Search II
Given an m x n board of characters and a list of strings words, return all words on the board. Each ...
- LeetCode: Word Ladder II 解题报告
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...
- 212. Word Search II
题目: Given a 2D board and a list of words from the dictionary, find all words in the board. Each word ...
随机推荐
- day01 前端bootstrap框架
day01 django框架之bootstrap框架 今日内容概要 前端框架之bootstrap 该框架支持cv编写前端页面 利用socket模块编写一个简易版本的web框架 利用wsgiref模块编 ...
- 商业爬虫学习笔记day5
一. 发送post请求 import requests url = "" # 发送post请求 data = { } response = requests.post(url, d ...
- hashtable深度探索
1.什么是哈希表(hashtable)?为什么要发明哈希表? 首先回答第二个问题,在之前的数据结构中我们学习了数组,链表,二叉树等数据结构,记录在结构中的相对位置是随机的,和记录的关键字之前不存在确定 ...
- 如果你不想让pthread_join阻塞你的进程,那么请调用pthread_detach
如果你不想让pthread_join阻塞你的进程,那么请调用pthread_detach 2016年01月13日 16:04:20 炸鸡叔 阅读数:7277 转发自:http://baike.ba ...
- Output of C++ Program | Set 4
Difficulty Level: Rookie Predict the output of below C++ programs. Question 1 1 #include<iostream ...
- java Map集合类
---恢复内容开始--- Map提供了一个更通用的元素存储方法,Map集合类用于存储元素对(称作"键"和"值"),其中每个键映射到一个值. 了解Map接口和方法 ...
- 深入 char
深入 char * ,char ** ,char a[ ] ,char *a[] 内核分类: c语言 2013-02-23 15:34 15176人阅读 评论(8) 收藏 举报Charcharchar ...
- 大数据处理系列之(一)Java线程池使用
前言:最近在做分布式海量数据处理项目,使用到了java的线程池,所以搜集了一些资料对它的使用做了一下总结和探究, 前面介绍的东西大多都是从网上搜集整理而来.文中最核心的东西在于后面两节无界队列线程池和 ...
- 【编程思想】【设计模式】【结构模式Structural】装饰模式decorator
Python版 https://github.com/faif/python-patterns/blob/master/structural/decorator.py #!/usr/bin/env p ...
- Druid数据库监控
一.简介 Druid是阿里开源的一个JDBC应用组件, 其包括三部分: DruidDriver: 代理Driver,能够提供基于Filter-Chain模式的插件体系. DruidDataSource ...