作者: 负雪明烛
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++)的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. leetcode 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

  6. 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 ...

  7. 【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 ...

  8. LeetCode: Word Ladder II 解题报告

    Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...

  9. 212. Word Search II

    题目: Given a 2D board and a list of words from the dictionary, find all words in the board. Each word ...

随机推荐

  1. Excel-姓名列中同一个人汇总金额列,得出总金额

    8.姓名列中同一个人求和金额列,得出总金额. 方法一: P2处公式=SUMPRODUCT(($M$2:$M$20=$M2)*($N$2:$N$20)) 解释函数: 引用:https://zhinan. ...

  2. 普通用户iptables规则持久化,开机自动恢复

    本文档对于docker环境下并不适用,docker环境的iptables持久化请参考https://www.cnblogs.com/wiseo/p/15000745.html 添加iptables规则 ...

  3. dubbo 协议的 K8s pod 存活探针配置

    背景 某项目采用微服务架构,dubbo 框架,K8s 方式部署. 其中 HTTP 协议由网关应用统一处理,大部分应用仅提供 dubbo 协议. 目标 应用某个实例(pod)状态异常时,尝试自动重启恢复 ...

  4. apostrophe

    apostrophe 者,', 0x27, 十进制39,ASCII里的single quote (单引号) 也.one of the 'inverted commas'. 在书写上可以表示所有格.省略 ...

  5. Learning Spark中文版--第四章--使用键值对(2)

    Actions Available on Pair RDDs (键值对RDD可用的action)   和transformation(转换)一样,键值对RDD也可以使用基础RDD上的action(开工 ...

  6. Mybatis逆向工程简单介绍

    转自:https://blog.csdn.net/yerenyuan_pku/article/details/71909325 什么是逆向工程 MyBatis的一个主要的特点就是需要程序员自己编写sq ...

  7. 记录一下使用MySQL的left join时,遇到的坑

    # 现象 left join在我们使用mysql查询的过程中可谓非常常见,比如博客里一篇文章有多少条评论.商城里一个货物有多少评论.一条评论有多少个赞等等.但是由于对join.on.where等关键字 ...

  8. Mockito 入门详解

    一个测试方法主要包括三部分: setup 执行操作 验证结果 public class CalculatorTest { Calculator mCalculator; @Before // setu ...

  9. C#获取Windows10屏幕的缩放比例

    现在1920x1080以上分辨率的高分屏电脑渐渐普及了.我们会在Windows的显示设置里看到缩放比例的设置.在Windows桌面客户端的开发中,有时会想要精确计算窗口的面积或位置.然而在默认情况下, ...

  10. 使用Modbus批量读取寄存器地址

    使用modbus单点读取地址是轮询可能会导致效率很低,频繁发送读取报文会导致plc响应时间拉长,批量读取可大大减少数据通信的过程,每次读取完成后,在内存中异步处理返回来的数据数组. modbus 功能 ...