leetcodegolang实现-----------------给出一个字符串,求出不重复的子字符串的最长长度

xiaoxiao2021-02-28  21

package main import ( "strings" ) /* 给出一个字符串,求出不重复的子字符串的最长长度 */ func lengthOfLongestSubstring(s string) int { if len(s) == 0 { return 0 } // 子字符串的头尾索引 i := 0 j := 1 // 最大长度 maxLen := 0 // 遍历这个字符串 for ; j < len(s); j++ { subStr := s[i:j] println(subStr) if pos := strings.IndexByte(subStr, s[j]); pos != -1 { if j-i > maxLen { maxLen = j - i i = i + pos + 1 } } } if maxLen == 0 { maxLen = len(s) } return maxLen }
转载请注明原文地址: https://www.6miu.com/read-2250139.html

最新回复(0)