05.Map 案例 滑动区间算法

下面是 Go 语言实现寻找最长不含有重复字符的子串的代码:

 滑动区间算法??
复制代码
package main

import "fmt"

func longestSubstringWithoutRepeating(s string) string {
    m := make(map[byte]int)
    start, maxLength, maxStart := 0, 0, 0

    for end := 0; end < len(s); end++ {
        if lastOccur, ok := m[s[end]]; ok && lastOccur >= start {
            start = lastOccur + 1
        }
        m[s[end]] = end
        if end-start+1 > maxLength {
            maxLength = end - start + 1
            maxStart = start
        }
    }

    return s[maxStart : maxStart+maxLength]
}

func main() {
    s := "ababcabcbbabcdefgddd"
    result := longestSubstringWithoutRepeating(s)
    fmt.Println("The longest substring without repeating characters is:", result) // 输出:abc
}
复制代码

 

posted @   JJJhr  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
点击右上角即可分享
微信分享提示