遇见YY

导航

 

原题:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters

package main

import "strings"


func lengthOfLongestSubstring(s string) int {
    if len(s) == 0 {
        return 0
    }
    Max := 1
    for i:=0; i< len(s) - 1; i++  {
        for j:=i+1; j < len(s); j++  {
            c := s[i:j]
            contains := strings.Contains(c, string(s[j]))
            if !contains {
                if  Max < len(c) + 1{
                    Max = len(c) + 1
                }
            }else {
                break
            }
        }
    }
    return Max
}




func main() {
    println(lengthOfLongestSubstring("aab"))
}

思路:对于每一轮,对于每一个字符,检查其前面的字符串是否包含这个字符,如果不包含,则更新Max,如果包含,则新开始一轮查找。

 

posted on 2020-12-13 15:41  一骑红尘妃子笑!  阅读(73)  评论(0编辑  收藏  举报