由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Leetcode的Substring with Concatenation of All Words超时。
相关主题
Leetcode第30题真心不容易lengthOfLongestSubstring 最后一个test case总是超时
4sum o(n^2)超时求点评:电话面试(今天第二天没有消息回复,感觉可能挂了)
LRU cache 超时一道电面题,分享下, 这个题应该用哪几个data structure?
帮忙看看为撒 leetcode OJ time out "Substring with Concatenation of All Words "请教一道题目
Substring with Concatenation of All Words 还有更简洁的解法吗?求DEBUG Substring with Concatenation of All Words
请问Substring with Concatenation of All Words?问大牛们一个Leetcode上的题
问一下OJ的Anagrams那道题Substring with Concatenation of All Words
星期一福利:某公司店面题Substring with Concatenation of All Words这题好麻烦
相关话题的讨论汇总
话题: string话题: integer话题: words话题: single话题: hashmap
进入JobHunting版参与讨论
1 (共1页)
c*****u
发帖数: 867
1
https://leetcode.com/problems/substring-with-concatenation-of-all-words/
这道题我是用的brute force,所以有时候超时。请问有更好的算法吗?
我的code有几次通过了,耗时200ms,我看最终结果里有很多几十毫秒的。请问那是怎
样的算法呢?
public class Solution {
public List findSubstring(String s, String[] words) {
ArrayList result = new ArrayList();
HashMap wordMap = new HashMap();
HashMap wordCover = new HashMap();
int wordsLen = words.length;
int singleLen = words[0].length();
boolean found;
for(int i=0; i if(!wordMap.containsKey(words[i])) {
wordMap.put(words[i], 1);
} else {
wordMap.put(words[i], wordMap.get(words[i]) + 1);
}
}
for(int i=0; i<=s.length() - wordsLen * singleLen; i++) {
found = true;
wordCover.clear();
for(int j=0; j String single = s.substring(i+j*singleLen, i+(j+1)*singleLen
);
if(!wordMap.containsKey(single)) {
found = false;
break;
}
if(!wordCover.containsKey(single)) {
wordCover.put(single, 1);
} else {
wordCover.put(single, wordCover.get(single) + 1);
}
if(wordCover.get(single) > wordMap.get(single)) {
found = false;
break;
}
}
if(found) {
result.add(i);
}
}
return result;
}
}
1 (共1页)
进入JobHunting版参与讨论
相关主题
Substring with Concatenation of All Words这题好麻烦Substring with Concatenation of All Words 还有更简洁的解法吗?
A家新鲜面经--都是经典题请问Substring with Concatenation of All Words?
请教LeetCode的3Sum问一下OJ的Anagrams那道题
LeetCode 的 4 sum 问题 如何用hash table做呢?星期一福利:某公司店面题
Leetcode第30题真心不容易lengthOfLongestSubstring 最后一个test case总是超时
4sum o(n^2)超时求点评:电话面试(今天第二天没有消息回复,感觉可能挂了)
LRU cache 超时一道电面题,分享下, 这个题应该用哪几个data structure?
帮忙看看为撒 leetcode OJ time out "Substring with Concatenation of All Words "请教一道题目
相关话题的讨论汇总
话题: string话题: integer话题: words话题: single话题: hashmap