Longest Substring Without Repeating Characters(Time Limit Exceeded)

来源:互联网 发布:java html5 视频播放 编辑:程序博客网 时间:2024/06/12 00:09

问题

Given a string, find the length of the longest substring without repeating characters.


测试用例

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring"pwke" is a subsequence and not a substring.



我的代码(Time Limit Exceeded)

func lengthOfLongestSubstring(s string) int {    var maxLength int    var tmpString string    maxLength = 0        lengthOfString := len(s)    //fmt.Println(lengthOfString)        for i := 0; i < lengthOfString; i++{        for j:= i + 1; j <= lengthOfString; j++{            if j == lengthOfString {                tmpString = s[i : ]            }else{                tmpString = s[i : j]            }            //fmt.Println(tmpString, j - i)            if checkRepeatingChar(tmpString) && (j - i) > maxLength{                maxLength = j - i            }        }    }        return maxLength}func checkRepeatingChar(s string) bool{    referenceOfs := s        for i := 0; i < len(s); i++{        for j := 0; j < len(s); j++{            //fmt.Println(s[i], referenceOfs[j])            if i == j {                continue            }else if s[i] == referenceOfs[j]{                return false            }        }    }        return true}



我的代码(Accepted)


阅读全文
0 0