File tree Expand file tree Collapse file tree 2 files changed +41
-17
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 2 files changed +41
-17
lines changed Original file line number Diff line number Diff line change 1+ """
2+ ๐ 141. Linked List Cycle
3+
4+ ๐ ๋ฌธ์ ์์ฝ
5+ - ์ฐ๊ฒฐ ๋ฆฌ์คํธ์ ์ฌ์ดํด(์ํ)์ด ์๋์ง ํ์ธํ๊ธฐ
6+ - ์์ผ๋ฉด True, ์์ผ๋ฉด False
7+
8+ ๐ฏ ํต์ฌ ์๊ณ ๋ฆฌ์ฆ
9+ - ํจํด: ํ๋ก์ด๋ ์ํ ํ์ง (ํ ๋ผ์ ๊ฑฐ๋ถ์ด)
10+ - ์๊ฐ๋ณต์ก๋: O(n)
11+ - ๊ณต๊ฐ๋ณต์ก๋: O(1)
12+
13+ ๐ก ํต์ฌ ์์ด๋์ด
14+ 1. slow๋ ํ ์นธ์ฉ, fast๋ ๋ ์นธ์ฉ ์ด๋
15+ 2. ์ฌ์ดํด์ด ์์ผ๋ฉด โ ๋์ด ์ธ์ ๊ฐ ๋ง๋จ!
16+ 3. ์ฌ์ดํด์ด ์์ผ๋ฉด โ fast๊ฐ ๋์ ๋๋ฌ (None)
17+ """
18+
19+ from typing import Optional
20+
21+
22+ class ListNode :
23+ def __init__ (self , val = 0 , next = None ):
24+ self .val = val
25+ self .next = next
26+
27+
28+ class Solution :
29+ def hasCycle (self , head : Optional [ListNode ]) -> bool :
30+ slow = head
31+ fast = head
32+
33+ while fast and fast .next :
34+ slow = slow .next # ๊ฑฐ๋ถ์ด: ํ ์นธ
35+ fast = fast .next .next # ํ ๋ผ: ๋ ์นธ
36+
37+ if slow == fast : # ๋ง๋ฌ๋ค!
38+ return True
39+
40+ return False # fast๊ฐ ๋์ ๋๋ฌ = ์ฌ์ดํด ์์
41+
Original file line number Diff line number Diff line change @@ -35,21 +35,4 @@ def lengthOfLongestSubstring(self, s: str) -> int:
3535 return max_len
3636
3737
38- # Set์ ์ฌ์ฉํ ๋ฐฉ์ (๋ ์ง๊ด์ )
39- class SolutionWithSet :
40- def lengthOfLongestSubstring (self , s : str ) -> int :
41- char_set = set ()
42- left = 0
43- max_len = 0
44-
45- for right in range (len (s )):
46- # ์ค๋ณต์ด ์ฌ๋ผ์ง ๋๊น์ง left ์ด๋
47- while s [right ] in char_set :
48- char_set .remove (s [left ])
49- left += 1
50-
51- char_set .add (s [right ])
52- max_len = max (max_len , right - left + 1 )
53-
54- return max_len
5538
You canโt perform that action at this time.
0 commit comments