187 Repeated DNA Sequences 重复的DNA序列
所有DNA由一系列缩写为A,C,G和 T 的核苷酸组成,例如:“ACGAATTCCG”。在研究DNA时,识别DNA中的重复序列有时非常有用。
编写一个函数来查找DNA分子中所有出现超多一次的10个字母长的序列(子串)。
详见:https://leetcode.com/problems/repeated-dna-sequences/description/
Java实现:
class Solution {
    public List<String> findRepeatedDnaSequences(String s) {
        List<String> res = new ArrayList<>();
        if(s.length()<10){
            return res;
        }
        Map<String,Integer> m = new HashMap<>();
        for(int i=0;i<s.length()-9;i++){
            String subString = s.substring(i,i+10);
            if(m.containsKey(subString)){
                int count=m.get(subString);  //如果为1,则添加进结果,否则继续遍历
                if(count==1){
                    res.add(subString);
                }
                m.put(subString,count+1);
            }else{
                m.put(subString,1);
            }
        }
        return res;
    }
}
C++实现:
class Solution {
public:
    vector<string> findRepeatedDnaSequences(string s) {
        vector<string> res;
        if (s.size() <= 10)
        {
            return res;
        }
        int mask = 0x7ffffff;
        unordered_map<int, int> m;
        int cur = 0, i = 0;
        while (i < 9)
        {
            cur = (cur << 3) | (s[i++] & 7);
        }
        while (i < s.size())
        {
            cur = ((cur & mask) << 3) | (s[i++] & 7);
            if (m.find(cur) != m.end())
            {
                if (m[cur] == 1)
                {
                    res.push_back(s.substr(i - 10, 10));
                }
                ++m[cur];
            }
            else
            {
                m[cur] = 1;
            }
        }
        return res;
    }
};
参考:https://www.cnblogs.com/grandyang/p/4284205.html
187 Repeated DNA Sequences 重复的DNA序列的更多相关文章
- 187. Repeated DNA Sequences重复的DNA子串序列
		[抄题]: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &qu ... 
- Leetcode187. Repeated DNA Sequences重复的DNA序列
		所有 DNA 由一系列缩写为 A,C,G 和 T 的核苷酸组成,例如:"ACGAATTCCG".在研究 DNA 时,识别 DNA 中的重复序列有时会对研究非常有帮助. 编写一个函数 ... 
- [LeetCode] 187. Repeated DNA Sequences 求重复的DNA序列
		All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ... 
- LeetCode 187. 重复的DNA序列(Repeated DNA Sequences)
		187. 重复的DNA序列 187. Repeated DNA Sequences 题目描述 All DNA is composed of a series of nucleotides abbrev ... 
- [LeetCode] Repeated DNA Sequences 求重复的DNA序列
		All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ... 
- leetcode   187. Repeated DNA Sequences  求重复的DNA串  ----------  java
		All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ... 
- [Swift]LeetCode187. 重复的DNA序列 | Repeated DNA Sequences
		All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ... 
- [LeetCode] 187. Repeated DNA Sequences 解题思路
		All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ... 
- 【LeetCode】187. Repeated DNA Sequences
		题目: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: " ... 
随机推荐
- SQL FULL OUTER JOIN 关键字
			SQL FULL OUTER JOIN 关键字 SQL FULL OUTER JOIN 关键字 FULL OUTER JOIN 关键字只要左表(table1)和右表(table2)其中一个表中存在匹配 ... 
- javascript 语法规范错误提示代码
			“Missing semicolon.” : “缺少分号.”, “Use the function form of \”use strict\”.” : “使用标准化定义function.”, “Un ... 
- jquery全选,取消全选
			近期项目又用到了这个全选和取消全选的操作. 曾经总是自己写纯JS.如今既然知道怎么写了.那怎样用JQ写得更简洁呢.这样也能学到新的东西.假设乎百度一下果然发现了好东东.感谢OSC的iuhoay. 代码 ... 
- android 之EditText输入检測
			近期开发一个功能的时候发生一个故事,其情节例如以下: 功能事实上不复杂,当中须要一个EditText来获取用户输入的信息.于是,我做了一个Dialog来显示我的输入界面(代码例如以下): mAlert ... 
- JAVA注解引发的思考
			自从JDK5開始Java開始添加了对元数据(MetaData)的支持,也就是注解(Annotation),到JDK7时已经有四种基本注解,新添加了一种@SafeVarargs. @Override注解 ... 
- [BZOJ2144]国家集训队 跳跳棋
			题目描述 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子. 我们用跳跳棋来做一个简单的游戏:棋盘上有3颗棋子,分别在a,b,c这三个位置.我们要通过最少的跳动把他们的位置移动 ... 
- js数组清空和去重
			1.splice var ary = [1,2,3,4]; ary.splice(0,ary.length); console.log(ary); // 输出 Array[0],空数组,即被清空了 2 ... 
- Lightoj 1012 - Guilty Prince
			bfs遍历一遍就行了. /* *********************************************** Author :guanjun Created Time :2016/6/ ... 
- Linux设备驱动--块设备(三)之程序设计
			块设备驱动注册与注销 块设备驱动中的第1个工作通常是注册它们自己到内核,完成这个任务的函数是 register_blkdev(),其原型为:int register_blkdev(unsigned i ... 
- HDU1054 Strategic Game —— 最小点覆盖 or 树形DP
			题目链接:https://vjudge.net/problem/HDU-1054 Strategic Game Time Limit: 20000/10000 MS (Java/Others) ... 
