代码拉取完成,页面将自动刷新
#
# @lc app=leetcode.cn id=3 lang=python3
#
# [3] 无重复字符的最长子串
#
# @lc code=start
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
rtn_vle = 0
chlid_str = ''
child_str_map = dict()
# target_str = dict()
# Judge whether s is legle
if s:
# Set initial value
strt_num = 0
chlid_str = s[0]
child_str_map[s[0]] = 0
last_vle_flg = False
# Use slide window to calculate target string
for tp_i,tp_str in enumerate(s):
if (tp_i + 1) == len(s):
last_vle_flg = True
# tp_str not in child_str
if (tp_str not in chlid_str) and \
tp_i > 0:
chlid_str = chlid_str + tp_str
# Judge last value
if last_vle_flg:
# target_str[chlid_str] = len(chlid_str)
if len(chlid_str) > rtn_vle:
rtn_vle = len(chlid_str)
elif tp_i > 0:
# target_str[chlid_str] = len(chlid_str)
if len(chlid_str) > rtn_vle:
rtn_vle = len(chlid_str)
strt_num = child_str_map[tp_str] + 1
chlid_str = s[strt_num:(tp_i+1)]
else:
# target_str[chlid_str] = 1
rtn_vle = 1
# Refresh child string map value
child_str_map[s[tp_i]] = tp_i
# Calculate return
return rtn_vle
# Test code
Test = Solution()
print(Test.lengthOfLongestSubstring(' '))
# @lc code=end
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。