2 Star 0 Fork 0

ShiboJiang / per_leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
3.无重复字符的最长子串.py 1.68 KB
一键复制 编辑 原始数据 按行查看 历史
Shibo Jiang 提交于 2020-01-12 01:03 . 添加 第三题 多解法
#
# @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
1
https://gitee.com/qq353838430/per_leetcode.git
git@gitee.com:qq353838430/per_leetcode.git
qq353838430
per_leetcode
per_leetcode
master

搜索帮助