Scj System Analyst

leetCode 题解

2021-12-15

本文用于收集 leetcode 题解,以训练思维,同时精进算法

无重复字符的最长子串

题目要求

func lengthOfLongestSubstring(s string) int {
    if len(s) == 0 {
        return 0
    }

    hash := make([]int, 127)
    for i, _ := range hash {
        hash[i] = -1
    }

    start, sz := -1, 0
    maxLen := 1
    for i := 0; i < len(s); i++ {
        j := hash[int(s[i])]
        if j > start {
            start = j
        }

        sz = i - start 

        if sz > maxLen {
            maxLen = sz
        }

        hash[int(s[i])] = i
    }

    return maxLen
}

【注意】本文属于作者原创,欢迎转载!转载时请注明以下内容:

(转载自)ShengChangJian's Blog编程技术文章地址:

https://ShengChangJian.github.io/2021/12/leetcode.html

主页地址:https://shengchangjian.github.io/


Similar Posts

上一篇: 算法基石

Comments

【目录】