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的DNA序列的问题,通过构建哈希映射和滑动窗口技术,高效地识别重复的DNA序列。

被折叠的 条评论
为什么被折叠?



