leetcode 日经贴,python code -repeated-dna-sequences

本文介绍了一个使用哈希表解决寻找字符串中重复长度为10的DNA序列的问题,通过构建哈希映射和滑动窗口技术,高效地识别重复的DNA序列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

class Solution:
    # @param s, a string
    # @return a list of strings
    def findRepeatedDnaSequences(self, s):
        map2Int = {'A':0, 'C':1, 'G':2, 'T':3}
        lst = []
        hah = {}
        if len(s) <= 10: return lst
        current = 0
        for i in range(10):
            current = current * 4 + map2Int[s[i]]
        hah[current] = False
        base = 4 ** 9
        for i in range(10, len(s)):
            current = (current - map2Int[s[i - 10]] * base) * 4 + map2Int[s[i]]
            if current not in hah:
                hah[current] = False
            elif hah[current] == False:
                hah[current] = True
                lst.append(s[i - 9 : i + 1])
        return lst


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值