Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3. 无重复字符的最长子串 #6

Open
yankewei opened this issue Feb 28, 2020 · 0 comments
Open

3. 无重复字符的最长子串 #6

yankewei opened this issue Feb 28, 2020 · 0 comments
Labels
中等 题目难度为中等 双指针 题目包含双指针解法 字符串 题目类型为字符串 滑动窗口 题目包含滑动窗口解法

Comments

@yankewei
Copy link
Owner

给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。

示例 1:

输入: "abcabcbb"
输出: 3 
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。

示例 2:

输入: "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。

示例 3:

输入: "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters

// 此题可用滑动窗口来解答,这个窗口不允许包含重复的字符。这样可以用指针 i 来表示窗口的左侧,j 来表示窗口的右侧,依次从左侧遍历,如果窗口中没有包含字符,则j++, i不变。如果 i++,j 不变。
func lengthOfLongestSubstring(s string) int {
    if s == "" {
        return 0
    }
    i, j, ret, l := 0, 0, 0, len(s)
    var maxS string
    for i < l && j < l {
        if !strings.ContainsRune(maxS, rune(s[j])) {
            j++
        } else {
            i++
        }
        maxS = s[i:j]
        if len(maxS) > ret {
            ret = len(maxS)
        }
    }
    return ret
}
@yankewei yankewei added 中等 题目难度为中等 双指针 题目包含双指针解法 字符串 题目类型为字符串 滑动窗口 题目包含滑动窗口解法 labels Feb 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
中等 题目难度为中等 双指针 题目包含双指针解法 字符串 题目类型为字符串 滑动窗口 题目包含滑动窗口解法
Projects
None yet
Development

No branches or pull requests

1 participant