已解决
LeetCode 187. 重复的DNA序列
来自网友在路上 175875提问 提问时间:2023-11-06 00:46:11阅读次数: 75
最佳答案 问答题库758位专家为你答疑解惑
原题链接:https://leetcode.cn/problems/repeated-dna-sequences/?envType=daily-question&envId=2023-11-05
用字符串哈希处理,可以在O(1)时间内查出前面是否有长度为10的相同串
时间复杂度为O(n) 空间复杂度O(n)
C++代码
class Solution {
public:typedef unsigned long long ull;const int P = 131;ull h[100000+5],p[100000+5];ull get(int l,int r){return h[r] - h[l-1]*p[r-l+1];}vector<string> findRepeatedDnaSequences(string s) {vector<string>res;unordered_map<ull,int>mp;p[0] = 1; // p^0 = 1h[0] = 0;s = " "+s;int n = s.length();for(int i=1;i<=n;i++){p[i] = p[i-1]*P;h[i] = h[i-1]*P + s[i];}int l = 1,r = 10;while(r<=n){ull key = get(l,r);if(mp[key]==1) res.push_back(s.substr(l,10));mp[key]++;l++,r++;}return res;}
};
查看全文
99%的人还看了
相似问题
猜你感兴趣
版权申明
本文"LeetCode 187. 重复的DNA序列":http://eshow365.cn/6-33162-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!
- 上一篇: acwing算法基础之数据结构--并查集算法
- 下一篇: 国际阿里云香港服务器!!!