LEETCODE - 1181【前后拼接】
class Solution {
public:
string gethead(string str){//获取头单词
string ret = "";
int strlen = str.length();
for(int i = 0; i < strlen; ++i){
if(str[i] == ' '){
break;
}
ret += str[i];
}
return ret;
}
string gettail(string str){//获取尾单词
int strlen = str.length();
int index = 0;
for(int i = strlen - 1; i >= 0; i--){
if(str[i] == ' '){
index = i;
break;
}
}
if(index == 0){
return str;
}
return str.substr(index+1,strlen);;
}
string deletesubstr(string str,string substr){//删除重复的单词【为了拼接】;只能删除第一个单词,不能删除最后一个单词【与空格有关】
string strT = str;
int pos = strT.find(substr);
if (pos >-1)
{
strT.erase(pos,substr.size());
}
return strT;
}
vector<string> beforeAndAfterPuzzles(vector<string>& phrases) {
int phraseslen = phrases.size();
vector<string> head;
vector<string> tail;
for(int i = 0; i < phraseslen; ++i){//获取头尾词的数组
head.push_back(gethead(phrases[i]));
tail.push_back(gettail(phrases[i]));
}
vector<string> ret;
for(int j = 0; j < phraseslen; ++j){//尾部
for(int k = 0; k < phraseslen; ++k){
if(j != k && tail[j] == head[k]){//如果某个子串的头等于另一个子串的尾
ret.push_back(phrases[j] + deletesubstr(phrases[k],head[k]));//把他拼接起来放在返回容器中
cout << phrases[j] + deletesubstr(phrases[k],head[k]) << endl;
}
}
}
sort(ret.begin(), ret.end());
ret.erase(unique(ret.begin(), ret.end()), ret.end());//去重
return ret;
}
};
LEETCODE - 1181【前后拼接】的更多相关文章
- LeetCode.1071-字符串最大公约数(Greatest Common Divisor of Strings)
这是小川的第391次更新,第421篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第253题(顺位题号是1071).对于字符串S和T,当且仅当S = T + ... + T ...
- 力扣Leetcode 179. 最大数 EOJ 和你在一起 字符串拼接 组成最大数
最大数 力扣 给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数. 示例 1: 输入: [10,2] 输出: 210 示例 2: 输入: [3,30,34,5,9] 输出: 9534330 说 ...
- Leetcode 321.拼接最大数
拼接最大数 给定长度分别为 m 和 n 的两个数组,其元素由 0-9 构成,表示两个自然数各位上的数字.现在从这两个数组中选出 k (k <= m + n) 个数字拼接成一个新的数,要求从同一个 ...
- Java实现 LeetCode 321 拼接最大数
321. 拼接最大数 给定长度分别为 m 和 n 的两个数组,其元素由 0-9 构成,表示两个自然数各位上的数字.现在从这两个数组中选出 k (k <= m + n) 个数字拼接成一个新的数,要 ...
- 【LeetCode】1181. Before and After Puzzle 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存首尾字符串 日期 题目地址:https://lee ...
- leetCode刷题(找到两个数组拼接后的中间数)
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- leetcode 321. 拼接最大数(单调栈,分治,贪心)
题目链接 https://leetcode-cn.com/problems/create-maximum-number/ 思路: 心都写碎了.... 也许就是不适合吧.... 你是个好人... cla ...
- [LeetCode] Repeated Substring Pattern 重复子字符串模式
Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...
- [LeetCode] Ternary Expression Parser 三元表达式解析器
Given a string representing arbitrarily nested ternary expressions, calculate the result of the expr ...
随机推荐
- ctfhub技能树—信息泄露—备份文件下载—网站源码
打开靶机 查看网页内容 使用dirsearch进行扫描 命令如下 python3 dirsearch.py -u http://challenge-91f1f5e6a791ab02.sandbox.c ...
- Redis-第五章节-8种数据类型
目录 一.Redis对key的操作 二.五种数据类型 String类型 List(集合) Set(集合) Hash(哈希) Zset(有序集合) 三.三种特殊数据类型 geospatial(地理位置) ...
- 如何安装快速 Docker 和 Docker-Compose 服务
最近由于个人在大家基于 Docker 的.企业级的CI/CD 环境,所以要安装 Docker 和 Docker-Compose ,这也算是一个学习过程,就把整个过程记录下来,便于以后查询. 测试环境 ...
- Android iText向pdf模板插入数据和图片
一.需求 这些日志在写App程序,有这么一个需求,就是需要生成格式统一的一个pdf文件,并向固定表格中填充数据,并且再在pdf中追加两页图片. 二.方案 手工设计一个pdf模板,这个具体步骤就不再赘述 ...
- matlab gui matlab gui 鼠标点击显示图像颜色值
首先看看效果 首先功能说明下,运行后通过myfile菜单打开一幅图片之后在axes中显示,由于要使用图片的放大缩小等功能将figure 的菜单栏与工具栏都一并打开了. 界面编程主要是callbac ...
- dotnet cli 5.0 新特性——dotnet tool search
dotnet cli 5.0 新特性--dotnet tool search Intro .NET 5.0 SDK 的发布,给 dotnet cli 引入了一个新的特性,dotnet tool sea ...
- How does Go kit compare to Micro?
Go kit - Frequently asked questions https://gokit.io/faq/ How does Go kit compare to Micro? Like Go ...
- hadoop 集群搭建 配置 spark yarn 对效率的提升永无止境 Hadoop Volume 配置
[手动验证:任意2个节点间是否实现 双向 ssh免密登录] 弄懂通信原理和集群的容错性 任意2个节点间实现双向 ssh免密登录,默认在~目录下 [实现上步后,在其中任一节点安装\配置hadoop后,可 ...
- 在IDEA中用三个jar包链接MongoDB数据库——实现增删改查
安装Robo 3T连接MongoDB数据库教程:https://blog.csdn.net/baidu_39298625/article/details/98845789 使用Robo 3T操作Mon ...
- Vue技术点整理-Vuex
什么是Vuex? 1,Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化 2,每一个 Vuex ...