【一天一道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 ...
随机推荐
- Node.js 模块系统
为了让Node.js的文件可以相互调用,Node.js提供了一个简单的模块系统. 模块是Node.js 应用程序的基本组成部分,文件和模块是一一对应的.换言之,一个 Node.js 文件就是一个模块, ...
- 用python爬了自己的微信,原来好友都是这样的!
偶然了解到Python里的itchat包,它已经完成了wechat的个人账号API接口,使爬取个人微信信息更加方便.鉴于自己很早之前就想知道诸如自己微信好友性别比例都来自哪个城市之类的问题,于是乎玩心 ...
- Docker 工具和示例
pipework Jérôme Petazzoni 编写了一个叫 pipework 的 shell 脚本,可以帮助用户在比较复杂的场景中完成容器的连接. playground Brandon Rhod ...
- MAC OS X下的Linux环境
关键字: HomeBrew,好比Windows下的Cygwin 安装Homebrew 该si胜过macport ruby -e "$(curl -fsSL https://raw.githu ...
- android M Launcher之LauncherModel (二)
上一篇我们通过LauncherModel的创建 ,实例化,以及与LauncherModel之间的沟通方式.初步了解了LauncherModel一些功能及用法,如果对LauncherModel一系列初始 ...
- PGM:贝叶斯网的参数估计
http://blog.csdn.net/pipisorry/article/details/52578631 本文讨论(完备数据的)贝叶斯网的参数估计问题:贝叶斯网的MLE最大似然估计和贝叶斯估计. ...
- IntelliJ Idea 设置 Dialyzer
IntelliJ Idea 设置 Dialyzer(金庆的专栏)Erlang开发使用IDEA IDE可以设置外部工具Dialyzer, 然后就可以直接Tools->External Tools ...
- Xcode无法安装基于ruby的插件问题的解决
Xcode有时需要安装一些第三方插件,很多插件是基于ruby的,确切的说是基于ruby gem的! 但是在国内有一个很尴尬的情况,就是官方的gems网站:https://rubygems.org 的安 ...
- 适配器模式(adapter)
适配器模式的定义: 将一个类的接口转换成客户希望的另外一个接口,适配器模式使得原本由于接口不兼容而不能在一起的那些类可以一起工作. 主要分为三类:类的适配器模式.对象的适配器模式.接口的适配器模式. ...
- 集合框架之Collection接口
Collection 层次结构中的根接口.Collection表示一组对象,这些对象也称为 collection 的元素.一些 collection 允许有重复的元素,而另一些则不允许.一些 coll ...