2023-11-05 LeetCode每日一题(重复的DNA序列)

2023-11-05每日一题

一、题目编号

复制代码
187. 重复的DNA序列

二、题目链接

点击跳转到题目位置

三、题目描述

DNA序列 由一系列核苷酸组成,缩写为 'A', 'C', 'G' 和 'T'.。

  • 例如,"ACGAATTCCG" 是一个 **DNA序列 **。

在研究 DNA 时,识别 DNA 中的重复序列非常有用。

给定一个表示 DNA序列 的字符串 s ,返回所有在 DNA 分子中出现不止一次的 长度为 10 的序列(子字符串)。你可以按 任意顺序 返回答案。

示例 1:

示例 2:

提示:

  • 0 <= s.length <= 105
  • s[i]=='A'、'C'、'G' or 'T'

四、解题代码

cpp 复制代码
class Solution {
    map<string, int> mp;
public:
    vector<string> findRepeatedDnaSequences(string s) {
        vector<string> res;
        int n = s.size();
        int right = 9;
        string temp;
        if(n < 10){
            return res;
        }
        for(int i = 0; i < 10; ++i){
            temp.push_back(s[i]);
        }
        mp[temp]++;
        while(right < n - 1){
            right++;
            temp.push_back(s[right]);
            reverse(temp.begin(), temp.end());
            temp.pop_back();
            reverse(temp.begin(), temp.end());
            if(mp[temp] == 1){
                res.push_back(temp);
            }
            mp[temp]++;
        }
    return res;
    }
};

五、解题思路

(1) 利用哈希表来统计字符串出现的数量。

(2) 利用滑动窗口来获得每次获得的字符串。

相关推荐
旖-旎7 小时前
深搜练习(组合)(5)
c++·算法·深度优先·力扣
@小码农7 小时前
2026年3月Scratch图形化编程等级考试一级真题试卷
开发语言·数据结构·c++·算法
Wect8 小时前
LeetCode 5. 最长回文子串:DP + 中心扩展
前端·算法·typescript
糖果店的幽灵8 小时前
决策树详解与sklearn实战
算法·决策树·sklearn
Lewiis8 小时前
趣谈排序算法
算法·排序算法
ComputerInBook8 小时前
数字图像处理(4版)——第 8 章——图像压缩与水印(上)(Rafael C.Gonzalez&Richard E. Woods)
人工智能·算法·计算机视觉·图像压缩·图像水印
刀法如飞8 小时前
Python列表去重:从新手三连到高阶特技,20种解法全收录
python·算法·编程语言
minji...8 小时前
算法题 动态规划
算法·动态规划
水蓝烟雨9 小时前
3337. 字符串转换后的长度 II
算法·leetcode
MegaDataFlowers9 小时前
SiliconCompiler workflow
算法