Longest Substring Without Repeat

2018-09-22  本文已影响0人  fjxCode

Longest Substring Without Repeating Characters

题目描述

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

思路:

解:

func lengthOfLongestSubstring(s string) int {
    sSlice := []rune(s)
    runeMap := make(map[rune]int,len(sSlice))
    curLen := 0
    maxLen := 0
    maxLenIdxStart := 0
    for i:=0;i<len(sSlice);i++{
        if _,ok := runeMap[sSlice[i]];ok{
            if curLen > maxLen {
                maxLen = curLen
            }
            curLen = i - runeMap[sSlice[i]]
            maxLenIdxStartNew := runeMap[sSlice[i]]+1
            //下面的操作会修改runeMap[sSlice[i]],要取值的操作先进行
            for j:= maxLenIdxStart;j< maxLenIdxStartNew;j++{
                delete(runeMap,sSlice[j])
            }
            maxLenIdxStart = maxLenIdxStartNew

            runeMap[sSlice[i]] = i

        }else {
            curLen++
            runeMap[sSlice[i]] = i
        }
    }
    //update
    if curLen > maxLen {
        maxLen = curLen
    }
    return maxLen
}
上一篇下一篇

猜你喜欢

热点阅读