LeetCode003__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.

为什么第一想法就是HashMap啊,O(n)的时间代价,恩,就这么干!
思路是这样的:
1、建立一个字母表,将字符串转换存入表中(建立HashMap)
2、删选重复的字母,找到最大子序列

第一次(¥很奇怪为啥要这样说,搞得像一次通不过一样¥):

class Solution(object):
    def lengthOfLongestSubstring(self, s):

        flag=[]
        temp=[]
        sub=[]
        for i in range(127):
            flag.append(0)

        for x in range(len(s)):
            num=ord(s[x])%127
            if flag[num]==0:
                flag[num]=1
                temp.append(s[x])
            elif len(sub)>=len(temp):
                temp=[]
            else:
                sub=temp[:]
                temp=[]

        return len(sub)

果然没通过
结果很奇怪啊
很奇怪,到底哪里错了呢?
原来犯了严重的逻辑错误,当最大序列持续到末尾时候,序列在temp中而没有传递到sub中。小改一下

class Solution(object):
    def lengthOfLongestSubstring(self, s):

        flag=[]
        temp=[]
        sub=[]
        for i in range(127):
            flag.append(0)

        for x in range(len(s)):
            num=ord(s[x])%127
            print(num)
            if flag[num]==0 and len(sub)<=len(temp):
                flag[num]=1
                temp.append(s[x])
                sub=temp[:]
                print(temp)

            else:
                temp=[]


        return len(sub)

第二次:
这里写图片描述

太打击人了
至少要两层循环,再来

class Solution(object):

    def lengthOfLongestSubstring(self, s):

        flag=[]
        for x in range(127):
            flag.append(-1)
        temp=[]

        sub=[]

        i=0
        while i<len(s):
            num=ord(s[i])%127
            print(s[i])

            if flag[num]==-1 and len(sub)<=len(temp):
                flag[num]=i
                temp.append(s[i])
                sub=temp[:]
                i=i+1
                print(temp)


            elif flag[num]==-1 and len(sub)>len(temp):
                flag[num]=i
                temp.append(s[i])
                i=i+1

            else:
                i=flag[num]+1
                temp=[]
                for x in range(127):
                    flag[x]=-1
                print(flag[num+1])

        return len(sub)

不说话了,直接上结果:
这里写图片描述

得找找其他方法了
试了试将复制步骤删除,直接统计数字:

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        dict={}
        t=0
        i=0
        sub=0
        while i<len(s):
            if s[i] in dict:
                i=dict[s[i]]+1
                dict={}
                t=0

            elif sub<t+1:
                dict[s[i]]=i
                t=t+1
                i=i+1
                sub=t
            else:
                dict[s[i]]=i
                t=t+1
                i=i+1
        return sub

这次通过了。
这里写图片描述

感觉应该不是这个问题,还是存在其他问题。(未完)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值