Q: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1.
解题思路:动态规划
class Solution:
# @param {string} s
# @return {integer}
def lengthOfLongestSubstring(self, s):
if s is None or len(s)==0:
return 0
if len(s)==1:
return 1
dic = {}
maxcount,startindex = 0,-1
for index,i in enumerate(s):
if i in dic and startindex<dic[i]:
startindex = dic[i]
if index-startindex>maxcount:
maxcount = index-startindex
dic[i]=index
return maxcount
本文介绍了一种使用动态规划解决字符串中寻找最长无重复字符子串长度的问题,并提供了一个Python实现示例。
336

被折叠的 条评论
为什么被折叠?



