【一天一道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 ...
随机推荐
- ACM Robot Motion
机器人已被编程为按照其指令中的路径进行操作.机器人要移动的下一个方向的指令放在网格中.可能的指令是 N north (up the page) S south (down the page) E ...
- CentOS7: How to resolve curl#56 - "Recv failure: Connection reset by peer"
Issue: When you execute Yum installation or update, you may encounter following error: Loaded plugin ...
- iOS中的颜色
最近在改Bug的时候,才注意到iOS 中的颜色竟然也大有文章,特来记录一下. 先说一下问题,因为某界面中有用xib实现的一个view,而这个view 只在UIColletionView的layout ...
- ROS新功能包PlotJuggler绘图
http://www.ros.org/news/2017/01/new-package-plotjuggler.html PlotJuggler,一个基于Qt的应用程序,允许用户加载,搜索和绘图数据. ...
- 【完整的App项目】颖火虫笔记v2
好久没写博客了,一方面是因为最近确实很忙,另一方面自己在改进颖火虫笔记这款App,在前面说过该App主要是模仿的印象笔记,(还不知道的,请看:http://blog.csdn.net/htq__/ar ...
- 使用Kubernetes需要注意的一些问题(FAQ of k8s)
本篇文章并不是介绍K8S 或者Docker的,而仅仅是使用过程中一些常见问题的汇总. 重启策略:http://kubernetes.io/docs/user-guide/pod-states/, 对于 ...
- Github Atom开源文本代码编辑器- 由 Github 打造的下一代编程开发利器
个人理解:Github 热度超凡的一个项目Atom,electron是整个atom的核心,对于electron可以理解成 electron =io.js + Chromium 通过 Electr ...
- Support Annotation Library使用详解
概述 Support Annotation Library是在Android Support Library19.1版本开始引入的一个全新的函数包,它包含了诸多有用的元注解.用来帮助开发者在编译期间发 ...
- Spring之WEB模块
Spring的WEB模块用于整合Web框架,例如Struts 1.Struts 2.JSF等 整合Struts 1 继承方式 Spring框架提供了ActionSupport类支持Struts 1的A ...
- 带你深入理解STL之Deque容器
在介绍STL的deque的容器之前,我们先来总结一下vector和list的优缺点.vector在内存中是分配一段连续的内存空间进行存储,其迭代器采用原生指针即可,因此其支持随机访问和存储,支持下标操 ...