【Leetcode】Repeated DNA Sequences
2016-05-23 18:02
489 查看
题目链接:https://leetcode.com/problems/repeated-dna-sequences/
题目:
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,
Given s = “AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT”,
Return:
[“AAAAACCCCC”, “CCCCCAAAAA”].
思路:
int是32位,ACTG可以用它们ascii码后三位区分,所以一个10个字符组成的子串只需要3*10=30位就能表示,遍历字符串所有长度为10的子串,将代表子串的int存入map中。
算法:
题目:
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,
Given s = “AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT”,
Return:
[“AAAAACCCCC”, “CCCCCAAAAA”].
思路:
int是32位,ACTG可以用它们ascii码后三位区分,所以一个10个字符组成的子串只需要3*10=30位就能表示,遍历字符串所有长度为10的子串,将代表子串的int存入map中。
算法:
public List<String> findRepeatedDnaSequences(String s) { List<String> list = 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) { list.add(s.substring(i - 9, i + 1)); map.put(key, 2); } } return list; }
相关文章推荐
- 让UITableViewCell的分隔线显示完全
- 21.UITextField
- Request获取用户真实IP
- Sketch设计UI的知识总结
- 动态规划——unique-paths
- 获取Request中Cookie的值
- We are unable to process your request. An unknown error occurred.
- UI配置的代码实现
- UGUI下拉列表的封装
- iOS 8 Auto Layout界面自动布局系列2-使用Xcode的Interface Builder添加布局约束
- Ural 1081 Binary Lexicographic Sequence
- 加速android studio build 速度
- Angular服务Request异步请求的详细分析
- 03.LoT.UI 前后台通用框架分解系列之——多样的表格
- UITextField 的clearButton
- java应用测试报告生成(二):利用ant的build.xml生成测试报告
- iOS学习之——UIStoryboard
- B - Longest Ordered Subsequence——HOJ
- 运行mysql5.x版本的时候,出现错误: [Err] 1366 - Incorrect integer value: '' for column 'Fbirthday' at row 1
- The request sent by the client was syntactically incorrect.