zoukankan      html  css  js  c++  java
  • 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。

    想了许久,然后做出来了一份答案,这个答案解决不了过长的数据,在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

  • 相关阅读:
    mybatis几种开发方式
    SpringData,JPA,MongoDB,Solr,Elasticsearch底层逻辑关系
    简论远程通信(RPC,Webservice,RMI,JMS的区别)
    spring/spring boot/spring mvc中用到的注解
    Centos常用命名
    Mybatis详解
    Java成长之路
    Hibernate 与Mybatis之比较
    Struts2 与SpringMVC之比较
    Maven 配置文件详解
  • 原文地址:https://www.cnblogs.com/fool-jingqiu/p/11206261.html
Copyright © 2011-2022 走看看