LeetCode 187. 重复的DNA序列

编程入门 行业动态 更新时间:2024-10-12 18:23:22

LeetCode 187. 重复的DNA<a href=https://www.elefans.com/category/jswz/34/1769864.html style=序列"/>

LeetCode 187. 重复的DNA序列

原题链接:/?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;}       
};

更多推荐

LeetCode 187. 重复的DNA序列

本文发布于:2023-11-16 12:13:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622124.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列   LeetCode   DNA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!