0x00 题目
Given a string, find the length of the longest substring without repeating characters.
Examples:
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.
0x01 分析
目的是想要截取到给定字符串中最长的不重复的字符串。
eg:
abcabcabc => abc
aaaaaaaababcd => abcd
aaaaaa => a
0x02 编码
- golang 版本
1234567891011121314151617181920func (s string) int {location := make([]int, 128)for i := 0; i < 128; i++ {location[i] = -1}begin, length := -1, 0for i, v := range s {if location[v] > begin{begin = location[s[i]]}if (i - begin > length) {length = i - begin;}location[s[i]] = i;}return length}
0x03 运行结果
语言 | 运行时间 |
---|---|
Golang | 9ms |
近期评论