【一天一道LeetCode】#30. Substring with Concatenation of All Words
注:这道题之前跳过了,现在补回来
一天一道LeetCode系列
(一)题目
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each >word in wordsexactly once and without any intervening characters.
For example, given:
s: “barfoothefoobarman”
words: [“foo”, “bar”]You should return the indices: [0,9].
(order does not matter).
(二)解题
本题需要在s中找到一串子串,子串是words中的单词按一定顺序排列组成的。返回这个子串在s中的起始位置。
主要思路:由于要考虑words中的重复单词,需要用到multiset,加快查找速度
1、首先用multiset存放words中的所有单词
2、遍历s,如果找到words中的某个单词,就在multiset中删除,直到multiset为空,如果没有找到则继续往后查找
3、返回下标的集合
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
multiset<string> cnt;//用来存放words中的单词
for (int i = 0; i < words.size(); i++)
{
cnt.insert(words[i]);//初始化
}
vector<int> ret;
int lenw = words[0].length();
int total = words.size() * lenw;
int i, j;
if (s.length() < total) return ret;
for (i = 0; i <= s.length() - total;i++)
{
multiset<string> tempcnt = cnt;//拷贝一个副本
for (j = i; j < s.length(); j += lenw)
{
string temp = s.substr(j, lenw);
auto iter = tempcnt.find(temp);
if (iter != tempcnt.end())//找到了
{
tempcnt.erase(iter);//先删除该元素
if (tempcnt.empty()) {//为空代表找到了words中的所有单词按一定顺序排列的子串
ret.push_back(i);
break;
}
}
else {//没找到
break;
}
}
}
return ret;
}
};
【一天一道LeetCode】#30. Substring with Concatenation of All Words的更多相关文章
- LeetCode - 30. Substring with Concatenation of All Words
30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...
- [LeetCode] 30. Substring with Concatenation of All Words 解题思路 - Java
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- leetCode 30.Substring with Concatenation of All Words (words中全部子串相连) 解题思路和方法
Substring with Concatenation of All Words You are given a string, s, and a list of words, words, tha ...
- [LeetCode] 30. Substring with Concatenation of All Words 串联所有单词的子串
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- Java [leetcode 30]Substring with Concatenation of All Words
题目描述: You are given a string, s, and a list of words, words, that are all of the same length. Find a ...
- [leetcode]30. Substring with Concatenation of All Words由所有单词连成的子串
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- LeetCode 30 Substring with Concatenation of All Words(确定包含所有子串的起始下标)
题目链接: https://leetcode.com/problems/substring-with-concatenation-of-all-words/?tab=Description 在字符 ...
- [LeetCode] 30. Substring with Concatenation of All Words ☆☆☆
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- [Leetcode][Python]30: Substring with Concatenation of All Words
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 30: Substring with Concatenation of All ...
随机推荐
- 作业03-面向对象入门.md
1. 本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识点组织起来.请使用工具画出本周学习到的知识点及知识点之间的联系.步骤如下: 1.1 写出你 ...
- iOS控制反转(IoC)与依赖注入(DI)的实现
背景 最近接触了一段时间的SpringMVC,对其控制反转(IoC)和依赖注入(DI)印象深刻,此后便一直在思考如何使用OC语言较好的实现这两个功能.Java语言自带的注解特性为IoC和DI带来了极大 ...
- 安卓高级3 RecyclerView 和cardView使用案例
cardView: 添加依赖:在Studio搜索cardview即可 在V7包中 或者直接在gradle中添加 compile 'com.android.support:cardview-v7:24. ...
- Dubbo框架应用之(二)--服务治理
Dubbo服务治理全貌图 当我们现有ITOO平台系统的业务随着用户的逐渐增大,设计的业务越来越广,系统会异常的复杂,在大规模服务之前,我们可以采用的是RMI或Hessian等工具,暴露和引用远程服务, ...
- RunLoop总结:RunLoop的应用场景(三)
今天要讲的RunLoop的应用场景可能太简单了,所以东西比较少.因为跟UITableView.UICollectionView等的滑动优化有关,就顺便总结一下会影响UITableView.UIColl ...
- Android支付——支付宝支付总结
摘要:分享牛系列.分享牛转载.第三方支付,java第三方支付.android第三方支付. 原文地址:http://blog.csdn.net/zwl5670/article/details/51219 ...
- YCSB性能测试工具使用
在网上查In-Memory NoSQL性能测试的资料时,偶然间发现了这个性能测试工具YCSB,全称为"Yahoo! Cloud Serving Benchmark".它内置了对常见 ...
- 浅谈SSH框架
在学习或者接触一个新的概念的时候,我们应该在脑海中发挥我们的搜索引擎,牵一发动全身的去想,这个知识跟我之前接触过的有哪些相同或者不同的地方,从这个角度去看那些新的知识和概念,经过旧知识和新知识的对比我 ...
- Android View框架总结(九)KeyEvent事件分发机制
请尊重分享成果,转载请注明出处: http://blog.csdn.net/hejjunlin/article/details/52335094 本篇开始分析按键消息事件分发(PS:本篇文章中源码均是 ...
- 百度地图开发之POI数据检索
前面学习百度地图的一些基本的用法,这次我们一起来看一看百度地图的检索功能吧 poi检索api的基本用法 百度地图的POI类中共有如下几个方法 PoiBoundSearchOption POI范围内检索 ...