LeetCode:Longest Substring Without Repeating Characters
第三题寻找最大的不包含重复字符的字符串。
借助了python的str查找函数,应该不是最优的解法。
#! /usr/bin/env python
# -*- coding:utf8 -*-
class Solution(object):
def lengthOfLongestSubstring(self, s):
""" :type s: str :rtype: int """
if len(s) == 0:
return 0
if len(s) == 1:
return 1
j = 0
sub_str = ""
max_len = 1
for i in range(0, len(s)):
index = sub_str.find(s[i])
if index == -1:
sub_str = s[j:i+1]
tmp_len = i - j + 1
if (max_len < tmp_len):
max_len = tmp_len
else:
j = j + index + 1
sub_str = s[j:i+1]
return max_len
if __name__ == "__main__":
s = Solution()
s1 = "bbtablud"
sub_len = s.lengthOfLongestSubstring(s1)
print sub_len
还没有评论,来说两句吧...