zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    bzoj 2816: [ZJOI2012]网络 (LCT 建多棵树)
    bzoj 2157: 旅游 (LCT 边权)
    bzoj 3669: [Noi2014]魔法森林 (LCT)
    bzoj 2049: [Sdoi2008]Cave 洞穴勘测 (LCT)
    bzoj 2002 : [Hnoi2010]Bounce 弹飞绵羊 (LCT)
    bzoj 3282: Tree (Link Cut Tree)
    模拟赛1
    Some tricks
    模拟赛简要题解与心得
    Noip 训练指南
  • 原文地址:https://www.cnblogs.com/xidian2014/p/8733860.html
Copyright © 2011-2022 走看看