美文网首页工作生活
Longest Substring Without Repeat

Longest Substring Without Repeat

作者: carlclone | 来源:发表于2019-07-01 13:58 被阅读0次

    这题用滑动窗口花了好久啊 , 可能晚上有点分心吧 , 差点就绝望了 , 主要是缩小窗口的时候没有维护好 left 指针和 map , 写伪代码的时候就写错了 ,
    复盘结果 : 下次写完伪代码直接用伪代码走几个能覆盖大部分情况的测试用例

    Leetcode 3. Longest Substring Without Repeating Characters Leetcode 3. Longest Substring Without Repeating Characters

    第一版将就下...至少比之前暴力破解快多了

    Leetcode 3. Longest Substring Without Repeating Characters Leetcode 3. Longest Substring Without Repeating Characters
    
    func lengthOfLongestSubstring(s string) int {
        max:=0
        j:=0
        k:=-1
        curWmap:=make(map[uint8]int)
        curWsize:=0
        endIndex:=len(s)-1
    
        for ;k<endIndex; {
            k++
            if _,ok:=curWmap[s[k]];!ok{
                curWmap[s[k]]= k
                curWsize++
                max=maxC(curWsize,max)
            } else {
                curWsize++
                orij:=j
                j=curWmap[s[k]]+1
                curWsize=curWsize-(j-orij)
                for ;orij<j;orij++ {
                    delete(curWmap,s[orij])
                }
                curWmap[s[k]]=k
                //sub:=curWmap[s[k]]
                //curWsize=curWsize - (sub-j)
                //orij:=j
                //curWmap[s[k]] = j
                //j=sub+1
                //
            }
        }
        return max
    
    }
    
    func maxC(l int,r int) int{
        if l>r {
            return l
        }
        return r
    }
    

    bobo sir的解 , 写伪代码走一遍他的思路

    Leetcode 3. Longest Substring Without Repeating Characters

    伪代码尝试走逻辑

    if  window next not overflow  and window next is not in map
        r++
        put s[r] into map  
    
    else
        window left step forward and remove from map (warn that r is not move ) , which is says that move left until no duplicate with s[r+1]
        
    then calculate the window size by left , right pointer ,  find out the max size with currentMax
    

    相关文章

      网友评论

        本文标题:Longest Substring Without Repeat

        本文链接:https://www.haomeiwen.com/subject/rregcctx.html