LeetCode——最长不重复子串

func lengthOfLongestSubstring(s string) int {
	arr := strings.Split(s, "")
	tempArr := []string{}
	max := 0
	curr := 0

	for _, val := range arr {
		index, contain := contains(tempArr, val)
		if !contain {
			tempArr = append(tempArr, val)
			curr += 1
		} else {
			tempArr = tempArr[index+1:]
			tempArr = append(tempArr, val)
			curr = curr - index
		}
		max = Max(max, curr)
	}

	return max
}
func contains(arr []string, val string) (int, bool) {
	for i, v := range arr {
		if v == val {
			return i, true
		}
	}
	return -1, false
}
发布了151 篇原创文章 · 获赞 72 · 访问量 29万+

猜你喜欢

转载自blog.csdn.net/liyuxing6639801/article/details/105624953