Skip to content

Commit 7952349

Browse files
committed
3
1 parent b2fa0de commit 7952349

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

LongestSubstringWithoutRepeatingCharacters/LongestSubstringWithoutRepeatingCharacters.cpp

+10-15
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
class Solution {
22
public:
33
int lengthOfLongestSubstring(string s) {
4-
// Start typing your C/C++ solution below
5-
// DO NOT write int main() function
6-
74
int hash[256];
8-
memset(hash, -1, sizeof(hash));
9-
int maxlen = 0, count = 0;
10-
for (int i = 0, j = 0; j < s.size(); j++) {
11-
if (hash[s[j]] == -1) {
12-
hash[s[j]] = j;
13-
maxlen = max(maxlen, j - i + 1);
14-
} else {
15-
while (i <= hash[s[j]]) {
16-
hash[s[i]] = -1;
17-
i += 1;
18-
}
19-
hash[s[j]] = j;
5+
for (int i = 0; i < 256; i++) {
6+
hash[i] = -1;
7+
}
8+
int maxlen = 0;
9+
int start = 0;
10+
for (int limit = 0; limit < s.size(); limit++) {
11+
if (hash[s[limit]] != -1) {
12+
start = max(start, hash[s[limit]] + 1);
2013
}
14+
maxlen = max(maxlen, limit - start + 1);
15+
hash[s[limit]] = limit;
2116
}
2217
return maxlen;
2318
}

0 commit comments

Comments
 (0)