#leetcode刷题之路30-串联所有单词的子串
给定一个字符串 s 和一些长度相同的单词 words。找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。
注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考虑 words 中单词串联的顺序。
示例 1:
输入:
s = "barfoothefoobarman",
words = ["foo","bar"]
输出:[0,9]
解释:
从索引 0 和 9 开始的子串分别是 "barfoor" 和 "foobar" 。
输出的顺序不重要, [9,0] 也是有效答案。
示例 2:
输入:
s = "wordgoodgoodgoodbestword",
words = ["word","good","best","word"]
输出:[]
思路:首先为了方便比较,把原vector和用于存储的目标vector都要排序,排序后就可以知道两者是否完全相等了;剩下的就是暴力循环;
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; vector<int> findSubstring(string s, vector<string>& words) {
sort(words.begin(),words.end());
vector<int> ans;
vector<string> temp;
int n=words.size();
if(n==) return ans;
int len=words[].length();
if(s.length()<n*len) return ans;
for(int i=;i<s.length()-n*len+;i++)
{
int t=i;
if(find(words.begin(),words.end(),s.substr(t,len))!=words.end())
{
temp.push_back(s.substr(t,len));
t=t+len;
}
for(int j=;j<n;j++)
{
if(find(words.begin(),words.end(),s.substr(t,len))!=words.end())
{
temp.push_back(s.substr(t,len));
t=t+len;
}
else
{
temp.clear();
break;
}
}
sort(temp.begin(),temp.end());
if(temp==words) ans.push_back(i);
temp.clear();
}
return ans;
} int main() {
string s="aaa";
vector<string> a={"a","a"};
vector<int> ans=findSubstring(s,a);
std::cout << ans.size() << std::endl;
//std::cout << ans[0]<<ans[1] << std::endl;
return ;
}
#leetcode刷题之路30-串联所有单词的子串的更多相关文章
- Java实现 LeetCode 30 串联所有单词的子串
30. 串联所有单词的子串 给定一个字符串 s 和一些长度相同的单词 words.找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置. 注意子串要与 words 中的单词完全匹配, ...
- python -- leetcode 刷题之路
第一题 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], tar ...
- 使用Java+Kotlin双语言的LeetCode刷题之路(二)
BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...
- 使用Java+Kotlin双语言的LeetCode刷题之路(三)
BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...
- 使用Java+Kotlin双语言的LeetCode刷题之路(一)
LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 1 两数之和 给定一个整数数 ...
- #leetcode刷题之路40-组合总和 II
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一次.说 ...
- #leetcode刷题之路38-报数
报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下:1. 12. 113. 214. 12115. 1112211 被读作 "one 1" (&quo ...
- #leetcode刷题之路16-最接近的三数之和
给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...
- #leetcode刷题之路13-罗马数字转整数
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M.字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写 ...
随机推荐
- 公司网络问题 & Caused by: org.gradle.internal.resource.transport.http.HttpRequestException
问题 公司网络问题,总是无法成功下载库,回到家就可以. Caused by: org.gradle.internal.resource.transport.http.HttpRequestExcept ...
- The formal parameters of the method
package basic.java; public class ParametersOfTheMethod { public static void main(String[] args) { in ...
- java中方法调用在内存中的体现
在java中,方法以及局部变量(即在方法中声明的变量)是放在栈内存上的.当你调用一个方法时,该方法会放在调用栈的栈顶.栈顶的方法是目前正在执行的方法,直到执行完毕才会从栈顶释放.我们知道,栈是一种执行 ...
- androidcookie存储sqllite
/**声明一些数据库操作的常量*/ private static SQLiteDatabase mDatabase = null; private static final String DATA ...
- c# 设计模式 之:工厂模式之---简单工厂
1.uml类图如下: 具体实现和依赖关系: 实现:SportCar.JeepCar.HatchbackCar 实现 Icar接口 依赖: Factory依赖 SportCar.JeepCar.Hatc ...
- d3js enter/exit深入了解
在 Data joins 章节我们演示了当data和dom element个数相同时的情况 <div id="content"> <div></div ...
- 乘风破浪:LeetCode真题_021_Merge Two Sorted Lists
乘风破浪:LeetCode真题_021_Merge Two Sorted Lists 一.前言 关于链表的合并操作我们是非常熟悉的了,下面我们再温故一下将两个有序链表合并成一个的过程,这是基本功. 二 ...
- December 25th 2016 Week 53rd Sunday
Patience is bitter, but its fruit is sweet. 忍耐是痛苦的,但它的果实是甜蜜的. What can we do if there is no fruit of ...
- December 19th 2016 Week 52nd Sunday
Truth and roses have thorns about them. 真理和玫瑰,身边都有刺. Either truth or roses, they all have thorns aro ...
- 软工团队 - 预则立&&他山之石
软工团队 - 预则立&&他山之石 团队任务计划 时间 人员 任务 10.23-10.29 张昭锡 初拟Android代码规范 李永盛 初拟PHP代码规范 刘晨瑶 初拟Git代码规范 刘 ...