https://leetcode-cn.com/problems/median-of-two-sorted-arrays/

代码:

# -*- coding:utf-8 -*-
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
st = {}
i, ans = 0, 0
for j in range(len(s)):
if s[j] in st:
i = max(st[s[j]], i)
ans = max(ans, j - i + 1)
st[s[j]] = j + 1
return ans



if __name__ == '__main__':
str1 = 'abcabcbb'
print(Solution().lengthOfLongestSubstring1(str1))

思路:记录相同字母上一次出现的位置,并使用变量ans记录这些位置的最大间距,即为整体字符串最长无重复子串。

分类: 算法

0 条评论

发表回复

Avatar placeholder

您的邮箱地址不会被公开。 必填项已用 * 标注