Skip to content

Commit 1f12369

Browse files
committed
feat: Upload longest-substring-without-repeating-characters(cpp)
1 parent 3df0940 commit 1f12369

File tree

1 file changed

+27
-0
lines changed
  • longest-substring-without-repeating-characters

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* https://leetcode.com/problems/longest-substring-without-repeating-characters/
3+
* 풀이방법: Sliding Window
4+
*
5+
* 공간 복잡도: O(1)
6+
* 시간 복잡도: O(n)
7+
*/
8+
9+
#include <vector>
10+
11+
class Solution {
12+
public:
13+
int lengthOfLongestSubstring(string s) {
14+
int answer = 0;
15+
vector<int> visit(256, -1);
16+
int i, j;
17+
18+
for (i = 0, j = 0; i < s.size(); i++){
19+
if (visit[s[i]] != -1 && visit[s[i]] >= j && visit[s[i]] < i) {
20+
j = visit[s[i]] + 1;
21+
}
22+
answer = max(answer, i - j + 1);
23+
visit[s[i]] = i;
24+
}
25+
return answer;
26+
}
27+
};

0 commit comments

Comments
 (0)