LeetCode: Repeated DNA Sequences
2015-03-06 01:35
267 查看
一定要用gcc调试。。。贪图vs的编辑方便,在本机上都过的case就是提交不能过。。。 换了gcc才发现问题。
static const char digit2char[4] = { 'A', 'C', 'G', 'T' };
//A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T
static const int char2digit[26] = {0,0,1,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,3};
int32_t encodeDNA(const string& s, int begin)
{
int32_t r = 0;
for (int i = begin; i < 10; i++){
r = r | (char2digit[s[i] - 'A'] << (2 * (9 - i)));
}
return r;
}
string decodeDNA(int32_t val)
{
char res[11] = { 0 };
for (int i = 0; i < 10; i++){
res[i] = digit2char[(val >> (2 * (9 - i))) & 3];
}
return string(res);
}
int32_t shiftRightByOne(int32_t val, char c)
{
int32_t res = ((val & 0x3ffff) << 2) |(char2digit[c-'A']);
return res;
}
vector<string> findRepeatedDnaSequences(string s) {
vector<string> res;
if (s.length() <= 10){
return res;
}
unordered_map<int32_t, size_t> checkMap;
set<int32_t> tmpRes;
int32_t encodeVal = encodeDNA(s, 0);
checkMap.insert(make_pair(encodeVal, 9));
for (int i = 10; i < s.length(); i++){
encodeVal = shiftRightByOne(encodeVal,s[i]);
auto it = checkMap.find(encodeVal);
if (it == checkMap.end()){
checkMap.insert(make_pair(encodeVal,i));
}
else{
tmpRes.insert(encodeVal);
}
}
for (auto& i : tmpRes){
res.push_back(decodeDNA(i));
}
return res;
}
static const char digit2char[4] = { 'A', 'C', 'G', 'T' };
//A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T
static const int char2digit[26] = {0,0,1,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,3};
int32_t encodeDNA(const string& s, int begin)
{
int32_t r = 0;
for (int i = begin; i < 10; i++){
r = r | (char2digit[s[i] - 'A'] << (2 * (9 - i)));
}
return r;
}
string decodeDNA(int32_t val)
{
char res[11] = { 0 };
for (int i = 0; i < 10; i++){
res[i] = digit2char[(val >> (2 * (9 - i))) & 3];
}
return string(res);
}
int32_t shiftRightByOne(int32_t val, char c)
{
int32_t res = ((val & 0x3ffff) << 2) |(char2digit[c-'A']);
return res;
}
vector<string> findRepeatedDnaSequences(string s) {
vector<string> res;
if (s.length() <= 10){
return res;
}
unordered_map<int32_t, size_t> checkMap;
set<int32_t> tmpRes;
int32_t encodeVal = encodeDNA(s, 0);
checkMap.insert(make_pair(encodeVal, 9));
for (int i = 10; i < s.length(); i++){
encodeVal = shiftRightByOne(encodeVal,s[i]);
auto it = checkMap.find(encodeVal);
if (it == checkMap.end()){
checkMap.insert(make_pair(encodeVal,i));
}
else{
tmpRes.insert(encodeVal);
}
}
for (auto& i : tmpRes){
res.push_back(decodeDNA(i));
}
return res;
}
相关文章推荐
- LeetCode Repeated DNA Sequences
- LeetCode – Refresh – Repeated DNA Sequences
- 算法作业HW15:LeetCode187 Repeated DNA Sequences
- LeetCode -- Repeated DNA Sequences
- [leetcode 187]Repeated DNA Sequences
- 【Leetcode】【Medium】Repeated DNA Sequences
- 【leetcode】Repeated DNA Sequences(middle)★
- [LeetCode] Repeated DNA Sequences
- [LeetCode] 187 - Repeated DNA Sequences
- leetcode之 Repeated DNA Sequences
- [leetcode][187]Repeated DNA Sequences
- Leetcode187: Repeated DNA Sequences
- Leetcode|Repeated DNA Sequences
- Leetcode Repeated DNA Sequences
- LeetCode 187 Repeated DNA Sequences
- Leetcode:Repeated DNA Sequences详细题解
- leetcode Repeated DNA Sequences 哈希表
- Repeated DNA Sequences @leetcode
- Leetcode: Repeated DNA Sequences
- Repeated DNA Sequences|leetcode题解