作者: 负雪明烛
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. 问题记录:SNP 标记 phasing

    GATK4 检测的SNP标记,有些位点会在检测过程中完成 phasing,在后续做基因型填充的时候有坑. GATK4 phasing 结果的缺失位点不是 ./. 也不是 .|.  而是直接变成一个单独 ...

  2. Notepad++—设置背景颜色

    之前,编程一直用的都是黑色背景色,最近发现,黑色背景色+高光字体,时间久了对眼睛特别不好.感觉自己编程到现在几年时间,眼睛就很不舒服,甚至有青光眼的趋势.所以,改用白底黑字,即"日间模式&q ...

  3. Excel-转换单元格格式的函数或“方法”汇总

    14.转换单元格格式的函数或"方法"汇总 =value(单元格)  #转换为数值 =A1&""                   #转换A1为文本 = ...

  4. A Child's History of England.25

    It was a September morning, and the sun was rising, when the King was awakened from slumber by the s ...

  5. Fllin(七)【Flink CDC实践】

    目录 FlinkCDC 1.简介 2.依赖 3.flink stream api 4.flink sql 5.自定义反序列化器 6.打包测试 FlinkCDC 1.简介 CDC是Change Data ...

  6. CORS 如果需要指定多个域名怎么办

    CORS 通过控制 Access-Control-Allow-Origin 控制哪些域名可以共享资源,取值如下 Access-Control-Allow-Origin: <origin> ...

  7. 【Linux】【Services】【SaaS】Docker+kubernetes(9. 安装consul实现服务注册发现)

    1. 简介 1.1. 官方网站: https://www.consul.io 1.2. Consul的功能: 服务发现:通过DNS或HTTP接口使得消费者发现服务,应用程序可以轻松找到所依赖的服务. ...

  8. Spring Boot,Spring Cloud,Spring Cloud Alibaba 版本选择说明以及整理归纳

    前言 本文的核心目的: 1.方便自己以后的查找,预览,参考 2.帮助那些不知道如何选择版本的朋友进行指引,而不是一味的跟风网上的版本,照抄. Spring Boot 版本 版本查询: https:// ...

  9. 【Java 8】函数式接口(二)—— 四大函数接口介绍

    前言 Java8中函数接口有很多,大概有几十个吧,具体究竟是多少我也数不清,所以一开始看的时候感觉一脸懵逼,不过其实根本没那么复杂,毕竟不应该也没必要把一个东西设计的很复杂. 几个单词 在学习了解之前 ...

  10. Linux内核配置-ARP系列

    all为所有,defalut为默认,其他为接口自己的 如果接口没填写,将会把defalut的值放接口上,实际生效的为all和接口中参数值较大的那个 #arp_ignore arp_ignore的参数含 ...