LeetCode() Repeated DNA Sequences 看的非常的过瘾!
2015-11-25 21:29
465 查看
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,
非常好的思路: 转换成位操作。
A -> 00
C -> 01
G -> 10
T -> 11
在编码的情况下,每10位字符串的组合即为一个数字,且10位的字符串有20位;一般来说int有4个字节,32位,即可以用于对应一个10位的字符串。例如
ACGTACGTAC -> 00011011000110110001
AAAAAAAAAA -> 00000000000000000000
20位的二进制数,至多有2^20种组合,因此hash table的大小为2^20,即1024 * 1024,将hash table设计为bool hashTable[1024 * 1024];
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"].
非常好的思路: 转换成位操作。
算法分析
首先考虑将ACGT进行二进制编码A -> 00
C -> 01
G -> 10
T -> 11
在编码的情况下,每10位字符串的组合即为一个数字,且10位的字符串有20位;一般来说int有4个字节,32位,即可以用于对应一个10位的字符串。例如
ACGTACGTAC -> 00011011000110110001
AAAAAAAAAA -> 00000000000000000000
20位的二进制数,至多有2^20种组合,因此hash table的大小为2^20,即1024 * 1024,将hash table设计为bool hashTable[1024 * 1024];
vector<string> findRepeatedDnaSequences(string s) { int hashMap[1048576] = {0}; vector<string> ans; int len = s.size(),hashNum = 0; if (len < 11) return ans; for (int i = 0;i < 9;++i) hashNum = hashNum << 2 | (s[i] - 'A' + 1) % 5; for (int i = 9;i < len;++i) if (hashMap[hashNum = (hashNum << 2 | (s[i] - 'A' + 1) % 5) & 0xfffff]++ == 1) ans.push_back(s.substr(i-9,10)); return ans; }
相关文章推荐
- 亚马逊CEO杰夫·贝佐斯拥有Blue Origin公司击败SpaceX 成为第一家成功重复使用火箭的宇航公司,美帝的电商果然比什么一堆千元旗舰吊多了
- CodeForces 545B Equidistant String
- 96 Unique Binary Search Trees
- UVA1511 Buy or Build 二进制枚举+最小生成树kruskal
- UIScrollView视差效果动画
- CodeUI Test:UIMap录制文件分析一
- require js 将config和入口函数分开写
- 1062. Talent and Virtue (25)
- 1062. Talent and Virtue (25)
- codeforces 602 D. Lipshitz Sequence (单调栈)
- Android stuido 出现找不到符号
- iOS之初识UI
- 如何创建一个UIImageView对象去显示图片
- UITableView——reloadData与reloadSection性能比较
- 053-19 What recommendations does the SQL Access Advisor provide for optimizing SQL queries? (Choose
- CSS media queries
- hdu 4740 The Donkey of Gui Zhou(dfs模拟好题)
- UINavigationBar字体颜色
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- iOS开发小技巧:刷新UITableView