File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ ํ์ด :
3+ ์ฌ๋ผ์ด๋ฉ ์๋์ฐ ๊ธฐ๋ฒ์ ์ฌ์ฉํด์ ํ์ด
4+ right๊ฐ ํ์ฌ๊น์ง ๋ฌธ์์ด์ ํฌํจ๋์ง ์๋ ๋ฌธ์๋ฉด right์ ๋ฌธ์๋ฅผ set์ ์ถ๊ฐ ํ right ์ฆ๊ฐ, ๋ฌธ์์ด๊ธธ์ด๋ฅผ ๋๋ฆฌ๊ณ ์ต๋๊ธธ์ด์ ๋น๊ตํด์ ์
๋ฐ์ดํธ
5+ ์ด๋ฏธ ๋ฌธ์์ด์ ํฌํจ๋๋ ๋ฌธ์๋ฉด left์ ๋ฌธ์๋ฅผ set์์ ์ ๊ฑฐ ํ left ์ฆ๊ฐ
6+
7+ ๋ฌธ์์ด ๊ธธ์ด : N
8+
9+ TC : O(N)
10+ ๋ฌธ์์ด ์ ์ฒด์ ๋ํด 1ํ ์ํ
11+
12+ SC : O(N)
13+ ํด์ํ
์ด๋ธ unordered_set์ ํฌ๊ธฐ๋ ๋ฌธ์์ด ๊ธธ์ด์ ๋น๋ก๋ก
14+ */
15+
16+ #include < unordered_set>
17+
18+ class Solution {
19+ public:
20+ int lengthOfLongestSubstring (string s) {
21+ int ans = 0 ;
22+ int left = 0 , right = 0 ;
23+ unordered_set<char > lookup;
24+
25+ while (right < s.size ())
26+ {
27+ if (lookup.find (s[right]) == lookup.end ()) {
28+ ans = max (ans, right - left + 1 );
29+ lookup.insert (s[right]);
30+ right++;
31+ }
32+ else {
33+ lookup.erase (s[left]);
34+ left++;
35+ }
36+ }
37+
38+ return ans;
39+ }
40+ };
You canโt perform that action at this time.
0 commit comments