超时代码

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. PowerShell并发控制-命令行参数之四问

    传教士问: win下如何 获取进程命令行,及命令行参数? 传教士答: 可以用这个powershell命令(实际上是wmi查询): (get-wmiobject -query "select ...

  2. VC单文档对话框添加托盘图标

    一 单文档添加托盘 1. 在CMainFrame中定义NOTIFYICONDATA结构m_notify 2.在OnCreate中添加托盘初始化代码 int CMainFrame::OnCreate(L ...

  3. [转]Vimium快捷键

    from: http://www.cppblog.com/deercoder/archive/2011/10/22/158886.html 今天下午折腾了一下Chrome下面的一个插件Vimium的使 ...

  4. Integer 和int

    获取Integer对象有两种方式:Integer x = 100:或者Integer x = new Integer(100): Integer x = 100:等价于Integer x = Inte ...

  5. 04-树5 Complete Binary Search Tree

    这题也是第二次做,本想第一次做时参考的算法会和老师讲的一样,不想老师讲的算法用在这题感觉还不如思雪园友的算法(http://www.cnblogs.com/sixue/archive/2015/04. ...

  6. 如何修改svn的密码或重新输入用户名密码

    在Eclipse 使用SVN 的过程中大多数人往往习惯把访问SVN 的用户名密码自动保存起来以便下次自动使用,不要再次手工输入,而此时(自动保存密码后),svn又不存在一个显式的登陆框了,但是有些时候 ...

  7. 一道面试题:按照其描述要求用java语言实现快速排序

    回来想了想,写出了如下的程序: /** * 一道面试题,按照其描述要求进行快速排序(英文的,希望理解是对的..) * 要求:和一般的快速排序算法不同的是,它不是依次交换pivot和左右元素节点(交换2 ...

  8. linux基础命令学习(一)

    pwd 输出当前工作路径tree 以树状图列出目录的内容ctrl+c 取消命令的执行clear 清空屏幕ls 列出文件目录 蓝色是目录,白色是普通文件alias cls=clear 别名终端:本地终端 ...

  9. php生成图片注释

    //生成验证码图片注释 <?php session_start(); $arr = array( 'a','b','c','d','e','f','g','h','i','j','k','l', ...

  10. PHP实现简单计算器

    <!--简单的计算器--> <!DOCTYPE html> <html> <head> <title>PHP实现简单计算器</titl ...