【LeetCode】1181. Before and After Puzzle 解题报告(C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/before-and-after-puzzle/
题目描述
Given a list of phrases, generate a list of Before and After puzzles.
A phrase is a string that consists of lowercase English letters and spaces only. No space appears in the start or the end of a phrase. There are no consecutive spaces in a phrase.
Before and After puzzles are phrases that are formed by merging two phrases where the last word of the first phrase is the same as the first word of the second phrase.
Return the Before and After puzzles that can be formed by every two phrases phrases[i]
and phrases[j]
where i != j
. Note that the order of matching two phrases matters, we want to consider both orders.
You should return a list of distinct strings sorted lexicographically.
Example 1:
Input: phrases = ["writing code","code rocks"]
Output: ["writing code rocks"]
Example 2:
Input: phrases = ["mission statement",
"a quick bite to eat",
"a chip off the old block",
"chocolate bar",
"mission impossible",
"a man on a mission",
"block party",
"eat my words",
"bar of soap"]
Output: ["a chip off the old block party",
"a man on a mission impossible",
"a man on a mission statement",
"a quick bite to eat my words",
"chocolate bar of soap"]
Example 3:
Input: phrases = ["a","b","a"]
Output: ["a"]
Constraints:
1 <= phrases.length <= 100
1 <= phrases[i].length <= 100
题目大意
给你一个「短语」列表 phrases,请你帮忙按规则生成拼接后的「新短语」列表。
「短语」(phrase)是仅由小写英文字母和空格组成的字符串。「短语」的开头和结尾都不会出现空格,「短语」中的空格不会连续出现。
「前后拼接」(Before and After puzzles)是合并两个「短语」形成「新短语」的方法。我们规定拼接时,第一个短语的最后一个单词 和 第二个短语的第一个单词 必须相同。
返回每两个「短语」 phrases[i] 和 phrases[j](i != j)进行「前后拼接」得到的「新短语」。
解题方法
保存首尾字符串
假如两个不同短语的首尾单词是相等的,就可以拼接到一起。因此重点在找到每个短语的首尾单词是什么。由于C++的string不支持split,使用了遍历的方法,找到第一个空格和最后一个空格出现的位置,从而分割出首单词head和尾单词tail,放入一个字典中。字典的格式是{字符串的索引:{首单词,尾单词}}
。
之后我们对每个短语都进行遍历查找其余的短语的头单词
是否等于当前短语的尾单词
,若相等则可以进行拼接。注意拼接的时候,重叠的首尾单词只需要保留其中一个。
最后,题目要我们返回排序了的拼接后的新短语,在C++中可以直接使用set进行排序。
C++代码如下:
class Solution {
public:
vector<string> beforeAndAfterPuzzles(vector<string>& phrases) {
const int N = phrases.size();
unordered_map<int, pair<string, string>> m;
for (int i = 0; i < N; ++i) {
string& phrase = phrases[i];
int first = -1;
int last = -1;
for (int j = 0; j < phrase.size(); ++j) {
if (phrase[j] == ' ') {
if (first == -1) {
first = j;
}
last = j;
}
}
string head = phrase.substr(0, first);
string tail = phrase.substr(last + 1);
m[i] = make_pair(head, tail);
}
set<string> s;
for (int i = 0; i < N; ++i) {
string& cur = phrases[i];
for (auto& other : m) {
if (other.first == i)
continue;
if (other.second.first == m[i].second) {
s.insert(cur + phrases[other.first].substr(m[i].second.size()));
}
}
}
return vector<string>(s.begin(), s.end());
}
};
日期
2019 年 9 月 20 日 —— 是选择中国互联网式加班?还是外企式养生?
【LeetCode】1181. Before and After Puzzle 解题报告(C++)的更多相关文章
- 【LeetCode】760. Find Anagram Mappings 解题报告
[LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】743. Network Delay Time 解题报告(Python)
[LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
- 【LeetCode】518. Coin Change 2 解题报告(Python)
[LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...
- 【LeetCode】474. Ones and Zeroes 解题报告(Python)
[LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)
[LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...
- 【LeetCode】732. My Calendar III解题报告
[LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...
随机推荐
- Nginx 编译 echo 模块
Nginx 编译 echo 模块 echo模块下载地址:https://github.com/openresty/echo-nginx-module 查看nginx已经编译的模块, nginx -V ...
- MySQL_集群
管理节点:192.168.31.66 sql节点1+data1节点:192.168.31.42 sql节点2+data2节点:192.168.31.128 llll
- 40-3Sum Closest
3Sum Closest My Submissions QuestionEditorial Solution Total Accepted: 76185 Total Submissions: 2621 ...
- 零基础学习java------day7------面向对象
1. 面向对象 1.1 概述 面向过程:c语言 面向对象:java :python:C++等等 面向对象的概念: (万物皆对象)------think in java everything in ...
- 【Reverse】每日必逆0x01
附件:https://files.buuoj.cn/files/7458c5c0ce999ac491df13cf7a7ed9f1/SimpleRev 题解 查壳 64位ELF文件,无壳 IDApro处 ...
- [学习总结]7、Android AsyncTask完全解析,带你从源码的角度彻底理解
我们都知道,Android UI是线程不安全的,如果想要在子线程里进行UI操作,就需要借助Android的异步消息处理机制.之前我也写过了一篇文章从源码层面分析了Android的异步消息处理机制,感兴 ...
- go 代理
环境变量中设置 #GO111MODULE=auto GOPROXY=https://goproxy.io 如果不第一次,则在命令行设置 go env -w GO111MODULE=on go env ...
- android获取路径目录方法
Environment常用方法: getExternalStrongeDirectory() 返回File,获取外部存储目录即SDCard getDownloadCacheDirectory() 返回 ...
- NSURLSessionDownloadTask实现大文件下载
- 4.1 涉及知识点(1)使用NSURLSession和NSURLSessionDownload可以很方便的实现文件下载操作 第一个参数:要下载文件的url路径 第二个参数:当接收完服务器返回的数据 ...
- 使用Spring JDBC连接数据库(以SQL Server为例)
一.配置Spring JDBC 1.导入相关jar包 (略) 2.配置文件applicationContext.xml <?xml version="1.0" encodin ...