力扣(leetcode 3)无重读字符的最长子串 python
2019-06-24 16:45
344 查看
题目
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
输入: “abcabcbb”
输出: 3
解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。
输入: “bbbbb”
输出: 1
解释: 因为无重复字符的最长子串是 “b”,所以其长度为 1。
输入: “pwwkew”
输出: 3
解释: 因为无重复字符的最长子串是 “wke”,所以其长度为 3。
请注意,你的答案必须是 子串 的长度,“pwke” 是一个子序列,不是子串。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters
思路
尺取法
代码
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: dic={} l=r=0 ans=0 res=0 while r < len(s): while r<len(s) and (s[r] not in dic or dic[s[r]]==0): dic[s[r]]=1 r+=1 res=r-l ans=max(res,ans) #print(r,l,ans) if r==len(s): return ans while l<r: if s[l]==s[r]: dic[s[l]]=1 l+=1 break else: dic[s[l]]=0 l+=1 res=r-l+1 ans=max(res,ans) r+=1 return ans
相关文章推荐
- LeetCode3. 无重复字符的最长子串(python)
- LeetCode3. python实现:无重复字符的最长子串问题☆☆
- Leetcode 刷题(python)之 无重复字符的最长子串
- LeetCode 3. 无重复字符的最长子串 Python
- LeetCode 3.Longest Substring Without Repeating Characters 无重复字符的最长子串 Python3 三种解法
- leetcode之3. 无重复字符的最长子串python实现
- LeetCode题解(python)-3.无重复字符的最长子串
- 力扣(leetcode 5)最长回文子串 python
- 用python写leetcode【13】 -- 至少有K个重复字符的最长子串(395)
- leetcode-无重复字符的最长子串(python)
- leetcode--无重复字符的最长子串--python
- Leetcode:3.无重复字符的最长子串
- Leetcode3. 无重复字符的最长子串
- LeetCode 无重复字符的最长子串
- LeetCode--无重复字符的最长子串
- LeetCode_Q3_无重复字符的最长子串
- LeetCode每日一题 010 无重复字符的最长子串
- LeetCode 3. 无重复字符的最长子串
- leetcode之旅—无重复字符的最长子串go语言版
- LeetCode-3 无重复字符的最长子串 Longest Substring Without Repeating Characters