LeetCode 3. Longest Substring Without Repeating Characters---Python实现
2018-01-29 01:53
483 查看
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given
which the length is 3.
Given "bbbbb", the answer is
with the length of 1.
Given
with the length of 3. Note that the answer must be a substring,
a subsequence and not a substring.
题意很明白,在一个字符串中找一个最长的不重复连续字串,选用一个字典来存储每个字符的位置,进行遍历即可。具体代码分析如下。class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
usechar={}
start=length=0 # /length=i=0
for i in range(len(s)): # for i in len(s):
if s[i] in usechar and start <=usechar[s[i]]: # if s[i] in usechar and start<=s[i]:
start=usechar[s[i]]+1# start=s[i]+1 #直接从重复的之后再来算啊。usechar里面的都是唯一的啊。
else:
length=max(length,i-start+1)
usechar[s[i]]=i;
return length
Examples:
Given
"abcabcbb", the answer is
"abc",
which the length is 3.
Given "bbbbb", the answer is
"b",
with the length of 1.
Given
"pwwkew", the answer is
"wke",
with the length of 3. Note that the answer must be a substring,
"pwke"is
a subsequence and not a substring.
题意很明白,在一个字符串中找一个最长的不重复连续字串,选用一个字典来存储每个字符的位置,进行遍历即可。具体代码分析如下。class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
usechar={}
start=length=0 # /length=i=0
for i in range(len(s)): # for i in len(s):
if s[i] in usechar and start <=usechar[s[i]]: # if s[i] in usechar and start<=s[i]:
start=usechar[s[i]]+1# start=s[i]+1 #直接从重复的之后再来算啊。usechar里面的都是唯一的啊。
else:
length=max(length,i-start+1)
usechar[s[i]]=i;
return length
相关文章推荐
- [LeetCode]3 Longest Substring Without Repeating Characters (C++,Python实现)
- leetcode(三)—— Longest Substring Without Repeating Characters(最长不重复子串 Python/C++)
- [LeetCode] Longest Substring Without Repeating Characters Python
- Leetcode 3. Longest Substring Without Repeating Characters The Solution of Python and Javascript
- leetcode-03-Longest Substring Without Repeating Characters-python
- [Leetcode][python]Longest Substring Without Repeating Characters
- leetCode之Longest Substring Without Repeating Characters实现
- leetcode Longest Substring Without Repeating Characters python
- Leetcode3:Longest Substring Without Repeating Characters@Python
- LeetCode题解(Golang实现)--Longest Substring Without Repeating Characters
- [leetcode]Longest Substring Without Repeating Characters(using python)
- LeetCode-3-Longest-Substring-Without-Repeating-Characters 尺取法+Python数组初始化+ASCLL转换
- [leetcode: Python]3. Longest Substring Without Repeating Characters
- 【leetcode】3. Longest Substring Without Repeating Characters(Python & C++)
- 3. Longest Substring Without Repeating Characters Leetcode Python New season for 2016
- leetcode 3. Longest Substring Without Repeating Characters (Python版)
- [LeetCode]Longest Substring Without Repeating Characters@python
- Leetcode -- Python -- Longest Substring Without Repeating Characters
- 【LeetCode-面试算法经典-Java实现】【003-Longest Substring Without Repeating Characters(最长非重复子字符串)】
- LeetCode Python - 3. Longest Substring Without Repeating Characters