LeetCode-187.Repeated DNA Sequences
2016-06-19 22:35
435 查看
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,
编码法 参考 https://segmentfault.com/a/1190000003922976
因为子串只有10位,每一位只有4种不同的字母,用4^10个数字来表示每种不同的序列,因为4^10=2^20<2^32所以我们可以用一个int来存储
其实可以不需要encode函数,因为10位的ACGT,每一个字符用两位bit表示就是20位,下一次的code值=取当前code末18位左移两位,然后加上新的char对应的bit
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"].
编码法 参考 https://segmentfault.com/a/1190000003922976
因为子串只有10位,每一位只有4种不同的字母,用4^10个数字来表示每种不同的序列,因为4^10=2^20<2^32所以我们可以用一个int来存储
int encode(string s, int i) { //A-0 C-1 G-2 T-3 int code = 0; for (int j = i; j < i+10; j++) { code *= 4; if (s[j] == 'C') code += 1; else if (s[j] == 'G') code += 2; else if (s[j] == 'T') code += 3; } return code; } vector<string> findRepeatedDnaSequences(string s) { int n = s.length()-10,code; vector<string> res; unordered_map<int,int> map; for (int i = 0; i <= n; i++) { code = encode(s,i); map[code]++; if (map[code] == 2) res.push_back(s.substr(i, 10)); } return res; }
其实可以不需要encode函数,因为10位的ACGT,每一个字符用两位bit表示就是20位,下一次的code值=取当前code末18位左移两位,然后加上新的char对应的bit
vector<string> findRepeatedDnaSequences(string s) { int n = s.length(); vector<string> res; unordered_map<int,int> map; unordered_map<char, int> dic; dic['A'] = 0; dic['C'] = 1; dic['G'] = 2; dic['T'] = 3; int i=0,code=0; while(i<9) code = (code << 2) + dic[s[i++]]; while(i<n) { code = ((code&0x3ffff) << 2) + dic[s[i++]]; map[code]++; if (map[code] == 2) res.push_back(s.substr(i-10, 10)); } return res; }
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解