0003-Longest Substring Without Repeating Characters

題目如下:

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

只求 pass 的解答。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
func lengthOfLongestSubstring(s string) int {
	m := make(map[rune]int)
	max := 0
	for i, r := range s {
		if d, has := m[r]; has {
			if max < len(m) {
				max = len(m)
			}

			for k, v := range m {
				if v < d {
					delete(m, k)
				}
			}
		}
		m[r] = i
	}

	if max < len(m) {
		max = len(m)
	}
	return max
}

過去曾用 c# 寫的解法。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class Solution {
    public int LengthOfLongestSubstring(string s) {
        int ans = 0;
        string tmp = string.Empty;
        
        for(int i = 0; i < s.Length; i++)
        {
            int j = tmp.IndexOf(s[i]);
            if (j == -1) tmp += s[i].ToString();
            else {
                ans = Math.Max(ans, tmp.Length);
                tmp = (j == tmp.Length ? string.Empty : tmp.Substring(j+1)) + s[i].ToString();
            }
        }
        
        return Math.Max(ans, tmp.Length);
    }
}
updatedupdated2021-06-172021-06-17