187. 重复的DNA序列

https://leetcode-cn.com/problems/repeated-dna-sequences/

解法一:

由于检测的是连续序列,因此只需i一次遍历0到n-10,每次取连续的十位[i:i+10],用哈希表即可。

class Solution:
    def findRepeatedDnaSequences(self, s: str) -> List[str]:
        n = len(s)
        res = []
        hashMap = {}  #键为字符串,值为出现次数
        for i in range(n-9):
            if s[i:i+10] in hashMap:
                hashMap[s[i:i+10]] += 1
            else:
                hashMap[s[i:i+10]] = 1
        for key in hashMap:
            if hashMap[key] > 1:
                res.append(key)
        return res

最后更新于