[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 useful to identify repeated sequences within the

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

  • 解题思路
    按照提示,采用Hash TableBit Manipulation来处理。
    备注:采用hash_map来做时,自己电脑上可以编译通过,但是LeetCode提示不存在hash_map对象。故改为使用map,但效率较低。
  • 实现代码
/*************************************************************
    *  @Author   : 楚兴
    *  @Date     : 2015/2/8 11:07
    *  @Status   : Accepted
    *  @Runtime  : 238 ms
*************************************************************/
#include <vector>
#include <iostream>
#include <string>
#include <map>
using namespace std;

class Solution {
public:
    vector<string> findRepeatedDnaSequences(string s) {
        vector<string> result;
        if (s.length() <= 10)
        {
            return result;
        }
        map<int,int> mymap;
        int i = 0;
        int cursor = 0;
        while (i < 9)
        {
            cursor = cursor << 3 | s.at(i) & 7;  //优先级顺序<<、&、|
            i++;
        }

        int mask = 0x7FFFFFF;
        while (i < s.length())
        {
            //cursor & mask得到27bit
            cursor = (cursor & mask) << 3 | s.at(i) & 7;  
            i++;
            auto it = mymap.find(cursor);
            if (it != mymap.end())
            {
                int count = (*it).second;
                if (count == 1)
                {
                    result.push_back(s.substr(i - 10, 10));
                }

                get<1>(*it) = count + 1; //更改second的值
            }
            else
            {
                mymap.insert(make_pair(cursor,1));
            }
        }

        return result;
    }
};

int main()
{
    Solution s;
    string str = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT";
    vector<string> result = s.findRepeatedDnaSequences(str);
    for (auto it = result.begin(); it != result.end(); it++)
    {
        cout<<(*it).c_str()<<endl;
    }
    system("pause");
}
目录
相关文章
|
5月前
|
算法 vr&ar 图形学
☆打卡算法☆LeetCode 187. 重复的DNA序列 算法解析
☆打卡算法☆LeetCode 187. 重复的DNA序列 算法解析
|
5月前
leetcode-187:重复的DNA序列
leetcode-187:重复的DNA序列
46 0
|
算法 C++
​LeetCode刷题实战187:重复的DNA序列
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
132 0
|
存储
LeetCode 187. Repeated DNA Sequences
所有 DNA 由一系列缩写为 A,C,G 和 T 的核苷酸组成,例如:“ACGAATTCCG”。在研究 DNA 时,识别 DNA 中的重复序列有时会对研究非常有帮助。 编写一个函数来查找 DNA 分子中所有出现超多一次的10个字母长的序列(子串)。
78 0
LeetCode 187. Repeated DNA Sequences
[LeetCode] Repeated DNA Sequences
This link has a great discussion about this problem. You may refer to it if you like. In fact, the idea and code in this passage is from the former link.
800 0
|
5天前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
44 6
|
2月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
82 2
|
5天前
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
|
2月前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
38 7