LeetCode OJ-- Substring with Concatenation of All Words ***
https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/
找S中子串,每个元素都在T中出现了,且所有T中元素都在S子串中出现了
class Solution {
public:
vector<int> findSubstring(string S, vector<string> &L) {
vector<int> ans;
if(S.empty() || S.size() == || L.empty() || L.size() == )
return ans; // L is longer
int len = L.size() * L[].size();
if(len > S.size())
return ans; unordered_map<string,int> dictL;
for(int i = ; i< L.size(); i++)
dictL[L[i]] += ; unordered_map<string,int> _dict;
for(int i = ; i + len <= S.size(); i++)
{
string subS = S.substr(i,len); _dict.clear();
bool flag = true; for(int j = ; j < len; j+= L[].size())
{
string subLittle = subS.substr(j,L[].size());
if(dictL.find(subLittle) == dictL.end()) //不属于目标里面的
{
flag = false;
break;
} _dict[subLittle] += ;
if(_dict[subLittle] > dictL[subLittle])
{
flag = false;
break;
}
}
if(flag)
ans.push_back(i); }
return ans;
}
};
超时、超时、超时、超时……
教训就是,有时候超时不是算法的问题,而是实现的不好。具体看下一篇博客
LeetCode OJ-- Substring with Concatenation of All Words ***的更多相关文章
- [LeetCode] 30. Substring with Concatenation of All Words 解题思路 - Java
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- leetCode 30.Substring with Concatenation of All Words (words中全部子串相连) 解题思路和方法
Substring with Concatenation of All Words You are given a string, s, and a list of words, words, tha ...
- 【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 - 30. Substring with Concatenation of All Words
30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...
- LeetCode 030 Substring with Concatenation of All Words
题目要求:Substring with Concatenation of All Words You are given a string, S, and a list of words, L, th ...
- [LeetCode] 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 (hard) ★
You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...
- Java for LeetCode 030 Substring with Concatenation of All Words【HARD】
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- Java [leetcode 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 a ...
- [leetcode]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 ...
随机推荐
- windows下CMD命令大全(仅供参考)
CMD命令:开始->运行->键入cmd或command(在命令行里可以看到系统版本.文件系统版本)chcp 修改默认字符集chcp 936默认中文chcp 650011. appwiz.c ...
- Python3爬取人人网(校内网)个人照片及朋友照片,并一键下载到本地~~~附源代码
题记: 11月14日早晨8点,人人网发布公告,宣布人人公司将人人网社交平台业务相关资产以2000万美元的现金加4000万美元的股票对价出售予北京多牛传媒,自此,人人公司将专注于境内的二手车业务和在美国 ...
- dijkstra与他的优化!!!
目录 SPFA已死,有事烧纸 Dijkstra 配对堆 引言 讲解 合并 修改 弹出堆顶pop 代码 结合! 1 2 @ SPFA已死,有事烧纸 其实我本人也是一个SPFA的忠诚用户,至少我的最少费用 ...
- 文件的特殊权限(SUID,SGID,SBIT)
文件的一般权限:r w x 对应 421 文件的特殊权限:SUID SGID SBIT对应 421 文件的隐藏权限:chattr设置隐藏权限,lsattr查看文件的隐藏权限. 文件访问控制列表: ...
- P2615 神奇的幻方
P2615 神奇的幻方 题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,……,N*N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可以通过以下方法构建一个幻方: 首 ...
- 【Jump Game II 】cpp
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- Windows核心编程小结3
内存映射和堆栈 内存映射文件 内存映射文件可以用于3个不同的目的: 系统使用内存映射文件,以便加载和执行.exe和DLL文件.这可以大大节省页文件空间和应用程序启动运行所需的时间. 可以使用内存映射文 ...
- winform DataGridView添加合计行
使用方法 /* DataTable dt= DBUtility.DB.FromSql(sql).ToDataTable(); DataGridViewAddSumRow sumRow = new Da ...
- MAC抓包工具charles(青花瓷)
下载链接:http://pan.baidu.com/s/1pL6ClBX 配置教程:http://blog.csdn.net/jiangwei0910410003/article/details/41 ...
- [AHOI2014&&JSOI2014][bzoj3876] 支线剧情 [上下界费用流]
题面 传送门 思路 转化模型:给一张有向无环图,每次你可以选择一条路径走,花费的时间为路径上边权的总和,问要使所有边都被走至少一遍(可以重复),至少需要花费多久 走至少一遍,等价于覆盖这条边 也就是说 ...