zoukankan      html  css  js  c++  java
  • 32.leetcode3_longest_substring_without_repeating_characters

    1.题目描述

    Given a string, find the length of the longest substring without repeating characters.

    返回给定字符串中的最长不重复字符串的长度

    2.题目分析

    起初看见这个题,直接双指针遍历了,结果老是wrong answer。然后用了建立新列表的方法,就通过了。

    3.解题思路

     1 class Solution(object):
     2     def lengthOfLongestSubstring(self, s):
     3         """
     4         :type s: str
     5         :rtype: int
     6         """
     7         longest=1
     8         temp=[]   #建立临时列表
     9         l=len(s)
    10         if l==0 or l==1: #首先考虑字符串长度为0和1
    11             return l
    12         temp.append(s[0])
    13         i=1
    14         while i<l:
    15             temp.append(s[i])
    16             j=0
    17             while j<len(temp)-1:
    18                 if temp[len(temp)-1]==temp[j]: #判断列表的首尾是否相同
    19                     del temp[0:j+1]  #截取不重复元素的列表
    20                     j=-1
    21                 j+=1
    22             longest=max(longest,len(temp)) #得到最长长度
    23             i+=1
    24         return longest #返回最长长度
  • 相关阅读:
    Java五
    Java I/O流
    第二周学习笔记
    第一周学习笔记
    第六次作业修改版
    第六周作业
    java第四次作业(补)
    java第五次作业
    Java第三次作业
    java第二次作业
  • 原文地址:https://www.cnblogs.com/19991201xiao/p/8460057.html
Copyright © 2011-2022 走看看