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 ...
随机推荐
- MSP430G2553之timerA产生PWM
总结:选SMCLK(可以测出来) 若选ACLK,经示波器PWM时有时无 举例一: #include <MSP430G2553.h> #define CPU_F ((doub ...
- dataTable/dataSet转换成Json格式
using System.Text;using System.Collections.Generic; 1)dataTable转Json(表格有名称:dt.TableName) public stat ...
- ansible quick start
1. ansible默认开启ControlPersist,也就是持续化ssh的socket连接,以跳过每次task都需要进行主机认证. 2. 但是centos的openssh版本太老了,不支持Cont ...
- JavaScript 时间特效 显示当前时间
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- poj1837 dp
//Accepted 2176 KB 47 ms //杠杆平横的条件:sum(c[i]*sum(g[j]))=0 // 所有的hook到原点的距离乘它上面挂着的物体的重量和的和为0 //对于一个hoo ...
- AudioQueue语音流 speex压缩 实时通讯 对讲机
参开 http://blog.csdn.net/liulina603/article/details/19029727 博客有多篇文章 http://blog.csdn.net/liulina6 ...
- 安全增强 Linux (SELinux) 剖析
架构和实现 Linux® 一直被认为是最安全的操作系统之一,但是通过引入安全增强 Linux(Security-Enhanced Linux,SELinux),National Security Ag ...
- intent 传参数
一.传递List<String>和List<Integer>以下以传递List<String>为例,发送List<String>语法为:intent.p ...
- MAC PHP MARK
这是一篇以 iOS 开发人员的视角写给广大iOS 程序猿的 PHP 入门指南.在这篇文章里我努力去发掘 objectiv-c 与 php 之间的共性,来帮助有一定 iOS 开发经验的攻城狮来快速上手一 ...
- Ubuntu安装软件提示”需要安装不能信任的软件包”解决办法
用 Ubuntu 安装输入法软件包时提示"需要安装不能信任的软件包","这个动作需要从没有授权的软件源来安装软件包",赋予权限执行仍然无法安装,上网查了一下,只 ...