187. Repeated DNA Sequences
2016-03-22 10:28
447 查看
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.
Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.
For example,
Subscribe to see which companies asked this question
关键在于把连着的5个字符用一个key表示,然后去查找有没有这样的一个key在hashmap中
Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.
For example,
Given s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT", Return: ["AAAAACCCCC", "CCCCCAAAAA"].
Subscribe to see which companies asked this question
关键在于把连着的5个字符用一个key表示,然后去查找有没有这样的一个key在hashmap中
public class Solution { public List<String> findRepeatedDnaSequences(String s) { //关键在于把连着的5个字符用一个key表示,然后去查找有没有这样的一个key在hashmap中 List<String> ans = new ArrayList<String>(); HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); int key = 0; for (int i = 0; i < s.length(); i++) { key = ((key << 3) | (s.charAt(i) & 0x7)) & 0x3fffffff; if (i < 9) continue; if (map.get(key) == null) { map.put(key, 1); } else if (map.get(key) == 1) { ans.add(s.substring(i - 9, i + 1)); map.put(key, 2); } } return ans; } }
相关文章推荐
- 60. Permutation Sequence
- 手机GUI自动化测试介绍 | 有道技术沙龙博客
- C#中怎么生成36位的GUID
- UIViewContentMode各类型效果
- iOS开发~UITableViewCell重用机制总结
- android UI进阶之实现listview的分页加载
- libraries for bitcode must be generated from Xcode Archive or Install build forARM7 解决办法
- DelayQueue 队列
- 《iOS Human Interface Guidelines》——Contact Add Button
- UIStepper
- SQL Interview Questions
- PHP基础之如何调试PHP程序(HBuilder)
- pywinauto首页、文档和下载 - GUI 自动化测试框架 - 开源中国社区
- 原生的强大DOM选择器querySelector
- Android UI自动化测试最佳实践 - 51Testing软件测试网-中国软件测试人的精神家园
- [leetcode] 308. Range Sum Query 2D - Mutable 解题报告
- request.getParameter() 和request.getAttribute() 区别
- UIView之setNeedsDisplay与drawRect 和 setNeedsLayout与layoutSubViews
- Integer Inquiry(大数)
- SOAPUI 接口测试学习笔记节选 结果写入操作方法介绍