Substring with Concatenation of All Words
来源:互联网 发布:好看的小说言情 知乎 编辑:程序博客网 时间:2024/06/03 14:51
参考小莹子同学的点击打开链接
自己刚才一直在纠结这段code,其实就是保证还剩下最后一个当前满足条件的word能留下。
if (curDict.get(temp) < dict.get(temp)) { count--; }
ArrayList<Integer> list = findSubstring("barfoofoobarthefoobarman",new String[]{"foo", "bar","the"});
用这个测一下吧。public class Solution { public List<Integer> findSubstring(String s, String[] words) { List<Integer> res = new LinkedList<>(); if (s == null || s.length() == 0 || words == null || words.length == 0) { return res; } int num = words.length; Map<String, Integer> dict = new HashMap<>(); for (String word: words) { if (dict.containsKey(word)) { dict.put(word, dict.get(word) + 1); } else { dict.put(word, 1); } } int wordLen = words[0].length(); for (int i = 0; i < wordLen; i++) { int index = i; int count = 0; Map<String, Integer> curDict = new HashMap<>(); //for (int j = i; j < s.length() - wordLen; j = j + wordLen) { for (int j = i; j <= s.length() - wordLen; j = j + wordLen) { String word = s.substring(j, j + wordLen); if (!dict.containsKey(word)) { //index = j; index = j + wordLen; curDict.clear(); count = 0; continue; } if (curDict.containsKey(word)) { curDict.put(word, curDict.get(word) + 1); } else { curDict.put(word, 1); } if (curDict.get(word) <= dict.get(word)) { count++; } else { while (curDict.get(word) > dict.get(word)) { String temp = s.substring(index, index + wordLen); curDict.put(temp, curDict.get(temp) - 1); if (curDict.get(temp) < dict.get(temp)) { count--; } index = index + wordLen; } } if (count == num) { res.add(index); String temp = s.substring(index, index + wordLen); curDict.put(temp, curDict.get(temp) - 1); count--; index = index + wordLen; } } } return res; }}
0 0
- LeetCode: Substring with Concatenation of All Words
- LeetCode : Substring with Concatenation of All Words
- LeetCode29: Substring with Concatenation of All Words
- [Leetcode] Substring with Concatenation of All Words
- Substring with Concatenation of All Words
- Substring with Concatenation of All Words
- [LeetCode]Substring with Concatenation of All Words
- Substring with Concatenation of All Words
- LeetCode-Substring with Concatenation of All Words
- [LeetCode] Substring with Concatenation of All Words
- 26 - Substring with Concatenation of All Words
- LeetCode:Substring with Concatenation of All Words
- Leetcode: Substring with Concatenation of All Words
- [LeetCode] Substring with Concatenation of All Words
- leetcode Substring with Concatenation of All Words
- leetcode Substring with Concatenation of All Words
- Substring with Concatenation of All Words
- LeetCode Substring with Concatenation of All Words
- Java之transient
- Git简单使用教程(三)-创建版本库
- Linux 静态库与共享库
- JM和x264是什么关系?
- 【VC小项目】-14.1排序函数模板
- Substring with Concatenation of All Words
- mysql分布式集群实现原理
- iOS之AFNetworking在POST时以二进制形式传输
- 58. Length of Last Word
- latex图表标题样式设置
- iOS开发数据存储篇—libsqlite3和FMDB的基本使用和区别
- linux安装opencv
- ios关于百度地图框架地理编码的封装;
- 浏览器探针--JavaScript