Leetcode 3. Longest Substring Without Repeating Characters

这题用滑动窗口花了好久啊 , 可能晚上有点分心吧 , 差点就绝望了 , 主要是缩小窗口的时候没有维护好 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
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!