-
Notifications
You must be signed in to change notification settings - Fork 14
/
3.cpp
36 lines (34 loc) · 810 Bytes
/
3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <queue>
#include <set>
#include <string.h>
using namespace std;
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int answer = 0;
queue<char> q;
set<char> st;
int count = 0;
if (s[0] == ' ' || s.size() == 1)
return 1;
q.push(s[0]);
st.insert(s[0]);
for (int i=1;i<s.size();i++){
if (st.find(s[i]) == st.end()){ //Áߺ¹x
q.push(s[i]);
st.insert(s[i]);
}
else {
while (q.front() != s[i]){
st.erase(st.find(q.front()));
q.pop();
}
q.pop();
q.push(s[i]);
}
if (q.size() > answer)
answer = q.size();
}
return answer;
}
};