由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Leetcode第30题真心不容易
相关主题
Leetcode的Substring with Concatenation of All Words超时。leetcode 438的难度 是不是标错了?
问大牛们一个Leetcode上的题问一个Anagram的参考程序
Substring with Concatenation of All Words 还有更简洁的解法吗?这个题目难度在leetcode里算什么
请问Substring with Concatenation of All Words?请教一道题目
帮忙看看为撒 leetcode OJ time out "Substring with Concatenation of All Words "Substring with Concatenation of All Words
求DEBUG Substring with Concatenation of All Words关于Leetcode: Substring with Concatenation of All Words
Dream company Onsite被搞了(少量面经)都来说说leetcode上无聊恶心的题吧
问一下OJ的Anagrams那道题Substring with Concatenation of All Words这题好麻烦
相关话题的讨论汇总
话题: string话题: str话题: integer话题: words话题: len
进入JobHunting版参与讨论
1 (共1页)
s******b
发帖数: 185
1
Substring with Concatenation of All Words
这个题,我做了多少次了,今晚看到,还是忘了思路。
不知道各位怎么样?
w********o
发帖数: 33
2
录成音频开车听 不信你还记不住
y**********u
发帖数: 2839
3
加油啊叔
w*******o
发帖数: 113
4
叔我来啦!
这道题就是说你先查一下words里每个单词的个数。注意每个单词都是等长的。
然后遍历字符串 像拿一根格尺,挨个比量一下,就可以了。
比如说:从 "barfoothefoobarman" 里找 ["foo", "bar"] 的链接
你先查一下,得到["foo": 1个, “bar” 1个]
然后遍历字符串
"barfoothefoobarman"
i
j
从i开始 截取3个字符是bar 然后你就 只需要再找["foo" 1个]
"barfoothefoobarman"
i
j
从j开始 截取3个是foo 然后就都找全了,可以把 i 放入结果里。
然后以每个i为开头的时候,都需要一个新的查数的表。
代码如下:
class Solution {
public List findSubstring(String s, String[] words) {
List res = new ArrayList<>();
int n = words.length, len = words[0].length();
Map map = new HashMap<>();
for (String w : words) map.put(w, map.getOrDefault(w, 0) + 1);
for (int i = 0; i <= s.length() - len * n; i++) {
Map m = new HashMap<>(map);
for (int j = 0; j < n; j++) {
String str = s.substring(i + j * len, i + j * len + len);
if (!m.containsKey(str)) break;
else {
m.put(str, m.get(str) - 1);
if (m.get(str) == 0) m.remove(str);
if (m.isEmpty()) res.add(i);
}
}
}
return res;
}
}
l********r
发帖数: 221
5
就是用sliding window的思路, 就不难了
v******s
发帖数: 144
6
这是anagram的变种
外层循环找字典里任意字的开始位置,step size 1
内层循环和Anagram完全一样 step size n
1 (共1页)
进入JobHunting版参与讨论
相关主题
Substring with Concatenation of All Words这题好麻烦帮忙看看为撒 leetcode OJ time out "Substring with Concatenation of All Words "
leetcode的anagram为什么用char array 做hashmap key就过不了呢?求DEBUG Substring with Concatenation of All Words
Google 需要bug free 么?Dream company Onsite被搞了(少量面经)
杯具!越改越差问一下OJ的Anagrams那道题
Leetcode的Substring with Concatenation of All Words超时。leetcode 438的难度 是不是标错了?
问大牛们一个Leetcode上的题问一个Anagram的参考程序
Substring with Concatenation of All Words 还有更简洁的解法吗?这个题目难度在leetcode里算什么
请问Substring with Concatenation of All Words?请教一道题目
相关话题的讨论汇总
话题: string话题: str话题: integer话题: words话题: len