您的位置:首页 > 科技 > IT业 > Java | Leetcode Java题解之第187题重复的DNA序列

Java | Leetcode Java题解之第187题重复的DNA序列

2024/10/5 14:08:40 来源:https://blog.csdn.net/m0_57195758/article/details/139910736  浏览:    关键词:Java | Leetcode Java题解之第187题重复的DNA序列

题目:

题解:

class Solution {static final int L = 10;Map<Character, Integer> bin = new HashMap<Character, Integer>() {{put('A', 0);put('C', 1);put('G', 2);put('T', 3);}};public List<String> findRepeatedDnaSequences(String s) {List<String> ans = new ArrayList<String>();int n = s.length();if (n <= L) {return ans;}int x = 0;for (int i = 0; i < L - 1; ++i) {x = (x << 2) | bin.get(s.charAt(i));}Map<Integer, Integer> cnt = new HashMap<Integer, Integer>();for (int i = 0; i <= n - L; ++i) {x = ((x << 2) | bin.get(s.charAt(i + L - 1))) & ((1 << (L * 2)) - 1);cnt.put(x, cnt.getOrDefault(x, 0) + 1);if (cnt.get(x) == 2) {ans.add(s.substring(i, i + L));}}return ans;}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com