• 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。


    想了许久,然后做出来了一份答案,这个答案解决不了过长的数据,在leetcode 上显示。。。“超出时间限制”,然后我觉得哈哈,心碎,不管怎样先记录下来的,日后慢慢思考更好的方式

    class Solution(object):
    def lengthOfLongestSubstring(self, s):
    """
    :type s: str
    :rtype: int
    """
    if len(s)>0:
    k=0
    dict_temp={} # 临时无重复字符串

    max_len=0 # 无重复字符串长度
    for i in range(0,len(s)): 
    temp=s[i]  # 默认无重复的字符串为是s[i]
    for j in range(i+1,len(s)):
    #print('temp before:',temp)
    if temp.find(s[j]) == -1: #如果找不到,就把新的字符添加到 临时字符串 temp
    temp=s[i:j+1]
    #print('temp=',temp)
    if j+1==len(s):     # 如果子串没有重复,一致计算到最后也要添加到dict中去
    print('temp=',temp,'j=',j)
    dict_temp[len(temp)]=temp
    k=1
    else:
    print(temp)   #如果包含字符串,则添加到临时dict中去
    dict_temp[len(temp)]=temp
    break
    if k==1:
    break
    if len(dict_temp)==0 and len(s)!=0: #排除 空字符串情况‘ ’和参数s全是无重复的情况
    return len(s)
    for key in dict_temp.keys():
    if key>max_len:
    max_len=key
    #print(dict_temp,key)
    return max_len
    else:
    return 0

  • 相关阅读:
    Python socket 基础(Server)
    Python socket 基础(Client)
    DOM
    Software Testing Concepts
    coroutine
    这一周~&&html+css的学习感悟
    充实的几天~
    时间不够用的感觉
    论文真痛苦
    焦躁的一周……
  • 原文地址:https://www.cnblogs.com/fool-jingqiu/p/11206261.html
Copyright © 2020-2023  润新知