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 ...
随机推荐
- iOS开发中XML的DOM和SAX解析方法
一.介绍 dom是w3c指定的一套规范标准,核心是按树形结构处理数据,dom解析器读入xml文件并在内存中建立一个结构一模一样的“树”,这树各节点和xml各标记对应,通过操纵此“树”来处理xml中的文 ...
- Apache目录结构(一)
一.Apache 目录结构 bin: 该目录用于存放apache常用的命令,比如httpd cig-bin:该目录存放linux下的常用命令 .sh conf:存放配置文件httpd.conf,在ht ...
- SYN Cookie的原理和实现
本文主要内容:SYN Cookie的原理,以及它的内核实现. 内核版本:3.6 SYN Flood 下面这段介绍引用自[1]. SYN Flood是一种非常危险而常见的Dos攻击方式.到目 ...
- druid简介
Druid首先是一个数据库连接池,但它不仅仅是一个数据库连接池,它还包含一个ProxyDriver,一系列内置的JDBC组件库,一个SQL Parser. 支持的数据库 Druid支持所有JDBC兼容 ...
- (spring-第16回【AOP基础篇】)基本概念
AOP(Aspect Oriented Programing),面向切面方程.介绍具体定义前,先看一个例子: package com.baobaotao.concept; public class F ...
- iOS开发之瞬间位移动画效果
步骤:1.使用single view application 创建一个新的项目 2.在.h文件中遵守<UIGestureRecognizerDelegate>协议,创建一个UIimagev ...
- php大力力 [035节] 先记录一些链接
[IT名人堂]专访百分点研发总监:不止于平台,大数据操作系统重磅来袭! [2015-8-11 14:17:04] [IT名人堂]专访1号店技术总监:大型电商网站的IT架构 [2015-8-25 15: ...
- 转载 javascript中的正则表达式总结 二
学习正则表达式 今年的第一篇javascript文章就是这个正则表达式了,之前的文章是转载别人的,不算自己的东西,可以忽略不计,最近突然想把转载别人的东西 统统删掉,因为转载过的文章,我根本没有从中获 ...
- Git的环境搭建
Git时当下流行的分布式版本控制系统. 集中式版本控制系统的版本库是集中存放在中央处理器的,所以开发者要先从中央服务器获取最新的版本,编码后再将自己的代码发送给中央处理器.集中式版本控制系统最大的缺点 ...
- C#静态类和静态成员
1. 静态类 1.1 简介 静态类和类成员用于创建无需创建类的实例就能够访问的数据和函数. 静态类成员可用于分离独立于任何对象标识的数据和行为:无论对象发生什么更改,这些数据和函数都不会随之变化. ...