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) 利用滑动窗口来获得每次获得的字符串。

相关推荐
wengqidaifeng9 小时前
数据结构(四)二叉树初步:计算机科学中的分叉树
c语言·数据结构
Once_day9 小时前
数据结构(2)常见概念
数据结构
独自破碎E9 小时前
【DFS】BISHI77数水坑
算法·深度优先
小陈phd17 小时前
多模态大模型学习笔记(七)——多模态数据的表征与对齐
人工智能·算法·机器学习
雨泪丶17 小时前
代码随想录算法训练营-Day35
算法
pursuit_csdn17 小时前
LeetCode 1022. Sum of Root To Leaf Binary Numbers
算法·leetcode·深度优先
NAGNIP18 小时前
一文搞懂神经元模型是什么!
人工智能·算法
董董灿是个攻城狮18 小时前
AI 视觉连载6:传统 CV 之高斯滤波
算法
踩坑记录19 小时前
leetcode hot100 35. 搜索插入位置 medium 二分查找
leetcode