关闭

Leetcode161: Repeated DNA Sequences

标签: leetcodealgorithm
163人阅读 评论(0) 收藏 举报
分类:

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"].

用Map的话超内存了,改用bitsmap,因为只有4个字母,所以只要用两位就可以做为一个字母的编码,10个字母就是20位,所以创建一个2^20大小的数组就可以解决问题了。

class Solution {
public:
    int chartoint(char a)
    {
        if(a == 'A') return 0;
        if(a == 'C') return 1;
        if(a == 'G') return 2;
        if(a == 'T') return 3;
    }
    vector<string> findRepeatedDnaSequences(string s) {
        vector<string> res;
        set<string> temp;
        string str;
        if(s.size()<10)
            return res;
        unsigned int val = 0;
        for(int i = 0; i < 9; i++)
        {
            val <<= 2;
            val |= chartoint(s[i]);
        }
        int map[1024*1024] = {0};
        for(int i = 9; i < s.size(); i++)
        {
            val <<= 14;
            val >>= 12;
            val |= chartoint(s[i]);
            map[val]++;
            if(map[val] > 1)
            {
                str = s.substr(i-9, 10);
                temp.insert(str);
            }
        }
        for(set<string>::iterator i = temp.begin(); i != temp.end(); ++i)
        {
            res.push_back(*i);
        }
        return res;
    }
};


0
0
查看评论

【LeetCode】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 som...
  • ljiabin
  • ljiabin
  • 2015-03-20 11:19
  • 5603

leetcode 187: Repeated DNA Sequences

Total Accepted: 1161 Total Submissions: 6887 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG...
  • xudli
  • xudli
  • 2015-02-09 07:29
  • 4647

[leetcode-187]Repeated DNA Sequences(java)

问题描述: 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 use...
  • zdavb
  • zdavb
  • 2015-08-28 22:20
  • 625

187. Repeated DNA Sequences Leetcode Python

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 ...
  • hyperbolechi
  • hyperbolechi
  • 2015-03-16 11:21
  • 1085

leetcode 187. 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 ...
  • JackZhang_123
  • JackZhang_123
  • 2017-09-20 09:44
  • 250

Leetcode 187 Repeated DNA Sequences 重复出现的DNA序列

编写一个程序来找到一个DNA分子中出现次数超过一次的长度为10的子序列(子串)。Tags: Hash Table, Bit Manipulation
  • smile_watermelon
  • smile_watermelon
  • 2015-08-05 15:54
  • 306

leetcode187. 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 ...
  • tinkle181129
  • tinkle181129
  • 2016-01-26 15:12
  • 161

[LeetCode]Repeated DNA Sequences,解题报告

目录目录 前言 题目 Native思路 二进制思路 AC前言最近在LeetCode上能一次AC的概率越来越低了,我这里也是把每次不能一次AC的题目记录下来,把解题思路分享给大家。题目All DNA is composed of a series of nucleotides abbreviated ...
  • zinss26914
  • zinss26914
  • 2015-03-14 12:09
  • 3452

LeetCode187——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 ...
  • booirror
  • booirror
  • 2015-08-18 23:47
  • 1013

Leetcode|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 ...
  • mike_learns_to_rock
  • mike_learns_to_rock
  • 2015-06-26 09:51
  • 125
    个人资料
    • 访问:84035次
    • 积分:2490
    • 等级:
    • 排名:第17420名
    • 原创:148篇
    • 转载:119篇
    • 译文:0篇
    • 评论:6条
    个人主页
    文章分类
    最新评论