LeetCode()Substring with Concatenation of All Words 为什么我的超时呢?找不到原因了!!!
超时代码
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
map<string,int> coll;
for(auto i:words)
coll[i]++;
vector<int> res;
int len=words[0].size(),sum=words.size();
for(int k=0;k<=s.length()-len*sum;k++)
if(check(s,k,len,sum,coll))
res.push_back(k);
return res;
}
bool check(string s,int start,int len,int sum,map<string,int> coll)
{
for(int i=start;i<=s.length()-len && sum!=0;i+=len)
{
string str=s.substr(i,len);
auto d=coll.find(str);
if(coll[str]>0 && d != coll.end())
{
coll[str]--;
sum--;
}
else
return false;
}
if(sum==0)
return true;
else
return false;
}
};
这个和我的没有什么区别吧?为什么就可以呢?
class Solution {
private:
int wordLen;
public:
vector<int> findSubstring(string S, vector<string> &L) {
unordered_map<string, int>wordTimes;
for(int i = 0; i < L.size(); i++)
wordTimes[L[i]]++;
wordLen = L[0].size(); vector<int> res;
for(int i = 0; i <= (int)(S.size()-L.size()*wordLen); i++)
if(helper(S, i, wordTimes, L.size()))
res.push_back(i);
return res;
} //判断子串s[index...]的前段是否能由L中的单词组合而成
bool helper(const string &s, int index,
unordered_map<string, int>wordTimes, int wordNum)
{
for(int i = index; wordNum != 0 && i <= (int)s.size()-wordLen; i+=wordLen)
{
string word = s.substr(i, wordLen);
auto ite = wordTimes.find(word);
if(ite != wordTimes.end() && ite->second > 0)
{ite->second--; wordNum--;}
else return false;
}
if(wordNum == 0)return true;
else return false;
}
};
LeetCode()Substring with Concatenation of All Words 为什么我的超时呢?找不到原因了!!!的更多相关文章
- LeetCode: Substring with Concatenation of All Words 解题报告
Substring with Concatenation of All Words You are given a string, S, and a list of words, L, that ar ...
- [LeetCode] 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:Substring with Concatenation of All Words (summarize)
题目链接 You are given a string, S, and a list of words, L, that are all of the same length. Find all st ...
- [leetcode]Substring with Concatenation of All Words @ Python
原题地址:https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/ 题意: You are given a ...
- Leetcode Substring with Concatenation of All Words
You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...
- [LeetCode] Substring with Concatenation of All Words(good)
You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...
- Leetcode:Substring with Concatenation of All Words分析和实现
题目大意是传入一个字符串s和一个字符串数组words,其中words中的所有字符串均等长.要在s中找所有的索引index,使得以s[index]为起始字符的长为words中字符串总长的s的子串是由wo ...
- LeetCode HashTable 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面试准备: Substring with Concatenation of All Words
leetcode面试准备: Substring with Concatenation of All Words 1 题目 You are given a string, s, and a list o ...
随机推荐
- 如何登录Google美国服务器
Google访问须知: ① 先访问一次 https://www.google.com/ncr ,禁止“国家重定向(No country Redirect) ” ② 再点击右上角齿轮图标,选第一项“Se ...
- [安卓]The Google Android Stack
- SharePoint安全 - 在Goolge和Bing中查找SharePoint相关内容
博客地址 http://blog.csdn.net/foxdave 本篇提供两个查询串字典,分别对应Google和Bing的搜索,用来查询SharePoint网站的相关内容 Google ShareP ...
- zookeeper3.4.6的安装
最近为了解决HDFS的单点故障的问题,采用了HA的方式是实现,并通过zookeeper来实现自动切换,既然需自动切换的话,那么必须要安装zookeeper,我选用的版本是3.4.6.下面详细介绍一下其 ...
- tableviewcell的取消选中,高亮
1.取消多余cell的分割线 UIView *view = [UIView new]; view.backgroundColor = [UIColor clearColor]; [tableView ...
- stm32 dac 配置过程
DAC模块的通道1来输出模拟电压,其详细设置步骤如下: 1)开启PA口时钟,设置PA4为模拟输入. STM32F103ZET6的DAC通道1是接在PA4上的,所以,我们先要使能PORTA的时钟,然后设 ...
- msf生成shellcode
msfpayload windows/exec CMD = calc.exe EXITFUNC=thread C 在kali Linux2.0新版中msfpayload命令已删除,功能已集成到msfv ...
- Mousejack测试指南
0x00 前言 近日,Bastille的研究团队发现了一种针对蓝牙键盘鼠标的攻击,攻击者可以利用漏洞控制电脑操作,他们将此攻击命名为MouseJack. 攻击者仅需要在亚马逊上以60美元购买设备,改造 ...
- 在国内时,更新ADT时需要配置的
RT
- PHP中使用的变量
变量是用于临时的存储值的容器.这些值可以是数字.文本,或者复杂得多的排列组合. 变量在任何编程语言中都居于核心地位,理解它们是使用PHP的关键所在.变量又是指在程序的运行过程中随时可以发生变化的量,是 ...