30. Substring with Concatenation of All Words
来源:互联网 发布:东莞网络 编辑:程序博客网 时间:2024/06/02 05:24
题意为给定字符串s,以及字符串数组words, 求s中包含words的连接子串(顺序无关)的所有索引。
正解:
public class Solution { public List<Integer> findSubstring(String s, String[] words) { if(words==null||words.length==0){ return null; } int lenth = words[0].length(); int size = words.length; List<Integer> result = new ArrayList<Integer>(); HashMap<String, Integer> tempSet = new HashMap<String, Integer>(); for(int i=0;i<words.length;i++){ if (tempSet.containsKey(words[i])) { tempSet.put(words[i], tempSet.get(words[i]) + 1); } else { tempSet.put(words[i], 1); } } for (int i = 0; i <= s.length() - size * lenth; i++) { if (check(s.substring(i, i + lenth * size), words, lenth,tempSet)) { result.add(i); } } return result; } public static boolean check(String s, String[] words, int length,HashMap<String,Integer> tempMap) { HashMap<String, Integer> tempSet = (HashMap<String, Integer>) tempMap.clone(); for(int i=0;i<words.length;i++){ String tmp=s.substring(i*length,i*length+length); if (!tempSet.containsKey(tmp)) { return false; } if (tempSet.get(tmp) == 1) { tempSet.remove(tmp); } else { tempSet.put(tmp, tempSet.get(tmp) - 1); } } return true; }}
初始解法为:
public class Solution { public List<Integer> findSubstring(String s, String[] words) { if(words==null||words.length==0){ return null; } int lenth = words[0].length(); int size = words.length; List<Integer> result = new ArrayList<Integer>(); HashMap<String, Integer> tempSet = new HashMap<String, Integer>(); for(int i=0;i<words.length;i++){ if (tempSet.containsKey(words[i])) { tempSet.put(words[i], tempSet.get(words[i]) + 1); } else { tempSet.put(words[i], 1); } } for (int i = 0; i <= s.length() - size * lenth; i++) { if (check(s.substring(i, i + lenth * size), words, lenth,tempSet)) { result.add(i); } } return result; } public static boolean check(String s, String[] words, int length,HashMap<String,Integer> tempMap) { HashMap<String, Integer> tempSet = (HashMap<String, Integer>) tempMap.clone(); while (!"".equals(s) ) { String tmp = s.substring(0, length); if (!tempSet.containsKey(tmp)) { return false; } if (tempSet.get(tmp) == 1) { tempSet.remove(tmp); } else { tempSet.put(tmp, tempSet.get(tmp) - 1); } s = s.substring(length); } return true; }}
将初始解法中的while循环改成for循环,重点是将string的剪切操作转为根据索引定位子串的方法,减少了大量的操作,节约时间。
解题体会:
初始存map存的是HashSet没考虑words可能重复的可能
初始子串s的遍历游标i是跳跃式增长,没考虑可能子串之间有重叠的可能
初始以s的子串为对象存的map,最后为节约时间以常量words来存map,且每次比较时使用了map的一个拷贝,速度较快
总结:
后续需要考虑问题时尽量全面
阅读全文
0 0
- 30. Substring with Concatenation of All Words
- 30. Substring with Concatenation of All Words
- 30.Substring with Concatenation of All Words
- 30. Substring with Concatenation of All Words
- 30. Substring with Concatenation of All Words
- 30. Substring with Concatenation of All Words
- 30. Substring with Concatenation of All Words
- 30. Substring with Concatenation of All Words
- 30.Substring with Concatenation of All Words
- 30. Substring with Concatenation of All Words
- 30. Substring with Concatenation of All Words
- 30. Substring with Concatenation of All Words
- 30. Substring with Concatenation of All Words
- 30. Substring with Concatenation of All Words
- 30. Substring with Concatenation of All Words
- 30. Substring with Concatenation of All Words
- 30. Substring with Concatenation of All Words
- 30. Substring with Concatenation of All Words
- sublime text3开发python并设置快捷键
- Educational Codeforces Round 25 B Five-In-a-Row 基础题
- offer面试题----1(字符串转换成整数)
- POJ 2054(Color a Tree-贪心)
- hdu 1317 spfa 算法 判断正环
- 30. Substring with Concatenation of All Words
- 对于程序员来说CPU是什么
- HTML+CSS编写静态网站-39 调整About页面
- Educational Codeforces Round 25 C Multi-judge Solving 贪心
- 《暗时间》
- Semaphore与多线程
- 《象与骑象人》
- 第 15 章日常系统维护
- offer面试题二-----寻找链表的倒数第K个节点