超时代码

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 为什么我的超时呢?找不到原因了!!!的更多相关文章

  1. 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 ...

  2. [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 ...

  3. 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 ...

  4. [leetcode]Substring with Concatenation of All Words @ Python

    原题地址:https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/ 题意: You are given a ...

  5. 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 ...

  6. [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 ...

  7. Leetcode:Substring with Concatenation of All Words分析和实现

    题目大意是传入一个字符串s和一个字符串数组words,其中words中的所有字符串均等长.要在s中找所有的索引index,使得以s[index]为起始字符的长为words中字符串总长的s的子串是由wo ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 如何在redhat下安装办公软件(openoffice)

    在redhat的client版本中自带有办公软件libreoffice,而在server版的redhat中却没有自带的办公软件,那么,如何在redhat的server版下安装办公软件呢? 方法一:配置 ...

  2. Gartner报告:多数CIO还未对数字化做好准备

    数字化经济时代已经来临.对于消费者而言,这意味着他们能够随时随地以更加丰富多彩的方式与虚拟世界和现实世界进行互动.对于企业而言,这意味着它们的运营将发生巨大变化,同时也有机会更加深入地了解客户并将这些 ...

  3. DB2事务日志已满的解决方法

    DB2命令终端输入: db2 update db cfg for <dbname> using LOGPRIMARY 50 db2 update db cfg for <dbname ...

  4. 分享第一次使用ProcessOn心得

    最近朋友推荐了我一款在线作图工具ProcessOn,感受使用了几天感觉确实很不错,在这里给大家分享一下! ProcessOn应该算的上是第一款完全免费在线作图工具,之前用过国外有类似的,不过都是付费的 ...

  5. SrcollView分页加载数据(布局)

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools=& ...

  6. 《java中局部变量和成员变量的区别》

    class Car { String color; int number; void run() { System.out.println(color+"::"+number); ...

  7. Android布局---相对布局

    Android布局分为五大类:相对布局.线性布局.表格布局.帧布局.网格布局 相对布局 语法格式: <RelativeLayout xmlns:android="http://sche ...

  8. R.java不能自动更新

    1. The type R is already defined. (很多时候我们在导入其他人的程序的时候,会遇到这个错误) 通常在project里有两个R.java,一个在src,一个在gen,通常 ...

  9. HDU5763 another meaning -(KMP+DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763 思路:dp[i]表示前i个字符组成的字符串所表示的意思数量,则当匹配时dp[i]=dp[i-1] ...

  10. PHP date和time

    一.time()函数 time():得到一个数字,这个数字表示从1970-01-01到现在共走了多少秒. 前一天的时间就是 time()-60*60*24. 前一年的时间就是 time()-60*60 ...