zoukankan      html  css  js  c++  java
  • (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters

    3. Longest Substring Without Repeating Characters

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

    Examples:

    Given "abcabcbb", the answer is "abc", which the length is 3.
    
    Given "bbbbb", the answer is "b", with the length of 1.
    
    Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring

    ANSWER:

     1 class Solution:
     2     def lengthOfLongestSubstring(self, s):
     3         """
     4         :type s: str
     5         :rtype: int
     6         """
     7         if s:
     8             i=0
     9             max_sub =s[0]
    10             len_s=len(s)
    11             while i<len(s):
    12                 j=i+1
    13                 for index in range(j,len_s):
    14                     if s[index] not in s[i:index]:
    15                         if len(s[i:index+1])>len(max_sub):
    16                             max_sub=s[i:index+1]
    17                     else:
    18                         break
    19                 i+=1
    20             return len(max_sub)
    21         else:
    22             return 0
    code
     1 def main(s):
     2     if s:
     3         i=0
     4         max_sub =s[0]
     5         len_s=len(s)
     6         while i<len(s):
     7             j=i+1
     8             for index in range(j,len_s):
     9                 if s[index] not in s[i:index]:
    10                     if len(s[i:index+1])>len(max_sub):
    11                         max_sub=s[i:index+1]
    12                 else:
    13                     break
    14             i+=1
    15         return len(max_sub)
    16     else:
    17         return 0
    18 if __name__ == '__main__':
    19     s='pwwke'
    20     print(main(s))
    调试代码
  • 相关阅读:
    【转】 Linux进程间通信
    Django中的Templates
    Django中的应用
    url的使用
    Django框架的使用
    Django的安装
    文件上传
    flask中的request和response
    模板
    静态文件处理
  • 原文地址:https://www.cnblogs.com/qflyue/p/8231191.html
Copyright © 2011-2022 走看看