下面是 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 }