【一天一道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 ...
随机推荐
- 关于基因组注释文件GTF的解释
GTF文件的全称是gene transfer format,主要是对染色体上的基因进行标注.怎么理解呢,其实所谓的基因名,基因座等,都只是后来人们给一段DNA序列起的名字而已,还原到细胞中就是细胞核里 ...
- 苹果OS系统安装Xcode方法
打开Xcode系统,在app store 里面找到自己系统对应的可升级的Xcode版本进行下载,下载到本地后,设置存放Xcode存放的文件夹为共享文件夹. 在MAC OS共享文件夹里面找到Xcode安 ...
- jQuery 效果 – 淡入淡出
在在jQuery中可以通过四个方法来实现元素的淡入淡出,这四个方法分别是:fadeIn().fadeOut().fadeToggle() 以及 fadeTo(),本文通过实例来为你讲解如何在jQuer ...
- Python3 CGI编程
什么是CGI CGI 目前由NCSA维护,NCSA定义CGI如下: CGI(Common Gateway Interface),通用网关接口,它是一段程序,运行在服务器上如:HTTP服务器,提供同客户 ...
- Bootstrap3 栅格系统-Less mixin 和变量
除了用于快速布局的预定义栅格类,Bootstrap 还包含了一组 Less 变量和 mixin 用于帮你生成简单.语义化的布局. 变量 通过变量来定义列数.槽(gutter)宽.媒体查询阈值(用于确定 ...
- Hadoop2动态调整Log级别-以datanode的heartbeat log为例
在Hadoop中,有些log信息在正常情况下是不打印出来的.比如datanode发送heartbeat的日志. 代码位于BPServiceActor#sendHeartBeat方法中,如下图: 由于默 ...
- Android TV开发总结(二)构建一个TV Metro界面(仿泰捷视频TV版)
前言:上篇是介绍构建TV app前要知道的一些事儿,开发Android TV和手机本质上没有太大的区别,屏大,焦点处理,按键处理,是有别于有手机和Pad的实质区别.今天来介绍TV中Metro UI风格 ...
- ROS(indigo) turtlebot2 + android一些有趣应用
ROS和Android配合使用非常有趣,这里推荐,ROSClinet,使用rosbridge让android和ROS通信: 具体参考奥斯卡的个人剧场:http://xxhong.net/ turtle ...
- EBS开发性能优化之查找需要优化的程序
1.登陆数据库LINUX环境 使用 top 命令查看进程状况 [oratest@ebsdb~]$top top - 15:58:59 up 8 days, 22:04, 1 user, load ...
- PGM:贝叶斯网表示之朴素贝叶斯模型naive Bayes
http://blog.csdn.net/pipisorry/article/details/52469064 独立性质的利用 条件参数化和条件独立性假设被结合在一起,目的是对高维概率分布产生非常紧凑 ...