您的位置:首页 > 产品设计 > UI/UE

LeetCode – Refresh – Repeated DNA Sequences

2015-03-22 11:03 267 查看
Follow the hint from https://leetcode.com/discuss/24478/i-did-it-in-10-lines-of-c
1. The last three digits of binary format will be :

A 001

C 011

G 111

T 100

So as 10 chars need 3 * 10 bits to record the status. We choose 0x3FFFFFF, still less than 32 bits.

9 chars: 0x1FFFFFFF

8 chars: 0xFFFFFF

7 0x1FFFFF

6 0x3FFFF

5 0x7FFF

4   0xFFF

3 0x1FF

2 0x3F

1 0x7

Code:

class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
vector<string> result;
unordered_map<int, int> record;
int current = 0, len = s.size();
for (int i = 0; i < len; i++) {
if (record[current = (current << 3 & 0x3FFFFFFF | s[i] & 7)]++ == 1) {
result.push_back(s.substr(i-9, 10));
}
}
return result;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: