Fork me on GitHub

187.Repeated DNA Sequences

class Solution {
	HashMap<String, Integer> hm;
	HashSet ls; 
	public List<String> findRepeatedDnaSequences(String s) {
		hm = new HashMap<String, Integer>();
		ls = new HashSet<String>();
		for (int i = 0; i < s.length(); i++) {                             // i + 10 <= s.length() 改进
			int j = i + 10;                                                    //降低复杂度   
			if (j <= s.length()) {                                          
				if (hm.containsKey(String.valueOf(s.substring(i, j)))&&hm.get(String.valueOf(s.substring(i, j)))!=2) {
					ls.add(String.valueOf(s.substring(i, j)));             //这里我也不知道为什么会出现重复 导致我用哈希表
					hm.replace(String.valueOf(s.substring(i, j)), 2);
				} else
					hm.put(String.valueOf(s.substring(i, j)), 1);
			}
		}
		return new ArrayList(ls);
	}
}
posted @ 2019-08-27 17:07  cznczai  阅读(95)  评论(0编辑  收藏  举报