42
42
``` python
43
43
class Solution :
44
44
def lengthOfLongestSubstring (self , s : str ) -> int :
45
- if not s:
46
- return 0
47
- cache = {}
48
- cache[s[0 ]] = 0
49
- dp = [0 for _ in s]
50
- dp[0 ] = res = 1
51
- for i in range (1 , len (s)):
52
- if s[i] == s[i - 1 ]:
53
- dp[i] = 1
54
- else :
55
- if cache.get(s[i]) is None :
56
- dp[i] = dp[i - 1 ] + 1
57
- else :
58
- dp[i] = min (dp[i - 1 ] + 1 , i - cache[s[i]])
59
- cache[s[i]] = i
60
- res = max (res, dp[i])
45
+ i = j = res = 0
46
+ chars = set ()
47
+ while i < len (s):
48
+ while s[i] in chars:
49
+ if s[j] in chars:
50
+ chars.remove(s[j])
51
+ j += 1
52
+ chars.add(s[i])
53
+ res = max (res, i - j + 1 )
54
+ i += 1
61
55
return res
62
56
```
63
57
@@ -66,100 +60,87 @@ class Solution:
66
60
``` java
67
61
class Solution {
68
62
public int lengthOfLongestSubstring (String s ) {
69
- if (s == null || " " . equals(s)) {
70
- return 0 ;
71
- }
72
- int n = s. length();
73
- char [] chars = s. toCharArray();
74
- int [] dp = new int [n];
75
- int res = 1 ;
76
- Map<Character , Integer > map = new HashMap<> ();
77
- dp[0 ] = 1 ;
78
- map. put(chars[0 ], 0 );
79
- for (int i = 1 ; i < n; ++ i) {
80
- if (chars[i] == chars[i - 1 ]) {
81
- dp[i] = 1 ;
82
- } else {
83
- if (map. get(chars[i]) == null ) {
84
- dp[i] = dp[i - 1 ] + 1 ;
85
- } else {
86
- dp[i] = Math . min(dp[i - 1 ] + 1 , i - map. get(chars[i]));
87
- }
63
+ int res = 0 ;
64
+ Set<Character > set = new HashSet<> ();
65
+ for (int i = 0 , j = 0 ; i < s. length(); ++ i) {
66
+ char c = s. charAt(i);
67
+ while (set. contains(c)) {
68
+ set. remove(s. charAt(j++ ));
88
69
}
89
- map . put(chars[i], i );
90
- res = Math . max(res, dp[i] );
70
+ set . add(c );
71
+ res = Math . max(res, i - j + 1 );
91
72
}
92
73
return res;
93
74
}
94
75
}
95
76
```
96
77
97
- ### ** JavaScript**
98
-
99
- ``` js
100
- /**
101
- * @param {string} s
102
- * @return {number}
103
- */
104
- var lengthOfLongestSubstring = function (s ) {
105
- let left = 0 ;
106
- let right = 0 ;
107
- let res = 0 ;
108
- let len = s .length ;
109
- let rec = {};
110
- while (right < len) {
111
- let tmp = " *" ;
112
- while (right < len) {
113
- tmp = s[right];
114
- if (! rec[tmp]) rec[tmp] = 0 ;
115
- rec[tmp]++ ;
116
- if (rec[tmp] > 1 ) break ;
117
- right++ ;
118
- }
119
- res = Math .max (res, right - left);
120
- while (rec[tmp] > 1 ) rec[s[left++ ]]-- ;
121
- right++ ;
122
- }
123
- return res;
124
- };
125
- ```
126
-
127
78
### ** C++**
128
79
129
80
``` cpp
130
81
class Solution {
131
82
public:
132
83
int lengthOfLongestSubstring(string s) {
133
- int arr[ 1024] ; // 本题的用例中,有不为小写字母的情况
134
- for (int i = 0; i < 1024; i++) {
135
- arr[ i] = -1;
84
+ int res = 0;
85
+ unordered_set<char > chars;
86
+ for (int i = 0, j = 0; i < s.size(); ++i)
87
+ {
88
+ while (chars.count(s[ i] ))
89
+ {
90
+ chars.erase(s[ j++] );
91
+ }
92
+ chars.insert(s[ i] );
93
+ res = max(res, i - j + 1);
136
94
}
95
+ return res;
96
+ }
97
+ };
98
+ ```
137
99
138
- int curLen = 0;
139
- int maxLen = 0;
140
-
141
- int len = s.size();
142
- for (int i = 0; i < len; i++) {
143
- int prev = arr[int(s[i])]; // 之前位置的index
144
- if (prev < 0 || i - prev > curLen) {
145
- // 其中,prev>0表示之前没有遇到过该字符
146
- // i - prev > curLen 表示之前遇到的当前字符,远超当前限定的范围
147
- // 这两种情况下,都是直接继续加就可以了
148
- curLen++;
149
- } else {
150
- if (curLen > maxLen) {
151
- maxLen = curLen;
152
- }
153
- curLen = i - prev; // curLen重新开始计数
154
- }
100
+ ### **Go**
101
+
102
+ ```go
103
+ func lengthOfLongestSubstring(s string) int {
104
+ chars := make(map[byte]bool)
105
+ res := 0
106
+ for i, j := 0, 0; i < len(s); i++ {
107
+ for chars[s[i]] {
108
+ chars[s[j]] = false
109
+ j++
110
+ }
111
+ chars[s[i]] = true
112
+ res = max(res, i-j+1)
113
+ }
114
+ return res
115
+ }
155
116
156
- arr[int (s[i])] = i;
157
- }
117
+ func max(a, b int) int {
118
+ if a > b {
119
+ return a
120
+ }
121
+ return b
122
+ }
123
+ ```
124
+
125
+ ### ** JavaScript**
158
126
159
- return maxLen > curLen ? maxLen : curLen;
127
+ ``` js
128
+ /**
129
+ * @param {string} s
130
+ * @return {number}
131
+ */
132
+ var lengthOfLongestSubstring = function (s ) {
133
+ let res = 0 ;
134
+ let chars = new Set ();
135
+ for (let i = 0 , j = 0 ; i < s .length ; ++ i) {
136
+ while (chars .has (s[i])) {
137
+ chars .delete (s[j++ ]);
138
+ }
139
+ chars .add (s[i]);
140
+ res = Math .max (res, i - j + 1 );
160
141
}
142
+ return res;
161
143
};
162
-
163
144
```
164
145
165
146
### ** ...**
0 commit comments