187 Repeated DNA Sequences 重复的DNA序列
所有DNA由一系列缩写为A,C,G和 T 的核苷酸组成,例如:“ACGAATTCCG”。在研究DNA时,识别DNA中的重复序列有时非常有用。
编写一个函数来查找DNA分子中所有出现超多一次的10个字母长的序列(子串)。
详见:https://leetcode.com/problems/repeated-dna-sequences/description/
Java实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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++实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 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
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步