File tree 5 files changed +193
-0
lines changed
5 files changed +193
-0
lines changed Original file line number Diff line number Diff line change
1
+ ### Intuition
2
+ - 재귀호출
3
+
4
+ ### Approach
5
+ - 재귀 호출
6
+ - 마지막에 left<>right 이동
7
+
8
+ ### Complexity
9
+ - Time complexity: O(n)
10
+ - Space complexity: O(n)
11
+
12
+
13
+ # Code
14
+
15
+ ``` python
16
+ # Definition for a binary tree node.
17
+ class TreeNode :
18
+ def __init__ (self , val = 0 , left = None , right = None ):
19
+ self .val = val
20
+ self .left = left
21
+ self .right = right
22
+
23
+ class Solution :
24
+ def invertTree (self , root : Optional[TreeNode]) -> Optional[TreeNode]:
25
+ if not root:
26
+ return None
27
+
28
+ left = self .invertTree(root.left)
29
+ right = self .invertTree(root.right)
30
+
31
+ root.left = right
32
+ root.right = left
33
+
34
+ return root
35
+
36
+
37
+ ```
Original file line number Diff line number Diff line change
1
+ ### Intuition
2
+
3
+ ### Approach
4
+ - pos 는 입력값이 아니다.
5
+ - 계속 회전하므로(detour), 언젠가는 one_way 로 가는 값과 만나게 됨.
6
+
7
+ ### Complexity
8
+ - Time complexity: O(n)
9
+ - Space complexity: O(1)
10
+
11
+
12
+ ### Code
13
+
14
+ ``` python
15
+ # Definition for singly-linked list.
16
+ class ListNode :
17
+ def __init__ (self , x ):
18
+ self .val = x
19
+ self .next = None
20
+
21
+ class Solution :
22
+ def hasCycle (self , head : Optional[ListNode]) -> bool :
23
+ if not head or not head.next:
24
+ return False
25
+
26
+ one_way = head
27
+ detour = head.next
28
+
29
+ while detour and detour.next:
30
+ if one_way == detour:
31
+ return True
32
+ one_way = one_way.next
33
+ detour = detour.next.next
34
+
35
+ return False
36
+ ```
Original file line number Diff line number Diff line change
1
+ ### Intuition
2
+
3
+ ### Approach
4
+
5
+ ### Complexity
6
+ - Time complexity: O(n)
7
+ - Space complexity: O(1)
8
+
9
+ ### Code
10
+
11
+ ``` python
12
+ # Definition for singly-linked list.
13
+ class ListNode :
14
+ def __init__ (self , val = 0 , next = None ):
15
+ self .val = val
16
+ self .next = next
17
+
18
+ class Solution :
19
+ def mergeTwoLists (self , l1 : Optional[ListNode], l2 : Optional[ListNode]) -> Optional[ListNode]:
20
+ if not l1:
21
+ return l2
22
+ if not l2:
23
+ return l1
24
+
25
+ merged_list = ListNode()
26
+ current = merged_list
27
+
28
+ while l1 and l2:
29
+ if l1.val <= l2.val:
30
+ current.next = l1
31
+ l1 = l1.next
32
+ else :
33
+ current.next = l2
34
+ l2 = l2.next
35
+
36
+ current = current.next # pointer setting.
37
+
38
+ current.next = l1 if l1 else l2
39
+
40
+ return merged_list.next
41
+ ```
Original file line number Diff line number Diff line change
1
+ ### Intuition
2
+ - 투 포인터 (prev, curr)
3
+
4
+ ### Approach
5
+ - 투 포인터
6
+ - 포인터를 이동하기 전에 prev 가 가리키는 (.next) 를 reverse 해 주어야 한다.
7
+
8
+ ### Complexity
9
+ - Time complexity: O(n)
10
+ - Space complexity: O(1)
11
+
12
+
13
+ ### Code
14
+
15
+ ``` python
16
+ # Definition for singly-linked list.
17
+ class ListNode :
18
+ def __init__ (self , val = 0 , next = None ):
19
+ self .val = val
20
+ self .next = next
21
+
22
+ class Solution :
23
+ def reverseList (self , head : Optional[ListNode]) -> Optional[ListNode]:
24
+ if not head:
25
+ return head
26
+
27
+ prev, curr = None , head
28
+
29
+ while curr:
30
+ next_node = curr.next
31
+ curr.next = prev
32
+ prev = curr
33
+ curr = next_node
34
+
35
+ return prev
36
+
37
+ ```
Original file line number Diff line number Diff line change
1
+ ### Intuition
2
+ - opening(` ([{ ` ) 스택을 쌓는다.
3
+ - 비교한다.
4
+
5
+ ### Approach
6
+ 1 . 먼저 입력받은 글자의 길이가 짝수가 아니면 무조건 ** False** .
7
+ 2 . for 문
8
+ - opening(` ([{ ` ) 이면 stack 에 넣는다.
9
+ - stack 이 비어있거나, 짝이 맞지 않으면(` is_parentheses()==False ` ) ** False** .
10
+ 3 . 다 완료되고 나서, 스택이 비어있다면 ** True** .
11
+
12
+
13
+ ### Complexity
14
+ - Time complexity: O(n)
15
+ - Space complexity: O(n)
16
+
17
+
18
+ ### Code
19
+
20
+ ``` python
21
+ class Solution :
22
+ def isValid (self , s : str ) -> bool :
23
+ stack = []
24
+ if len (s) % 2 != 0 :
25
+ return False
26
+ for word in s:
27
+ if word in ' ([{' :
28
+ stack.append(word)
29
+ elif not stack or is_parentheses(stack.pop(), word) is False :
30
+ return False
31
+ return not stack
32
+
33
+
34
+ def is_parentheses (pointer , word ) -> bool :
35
+ if pointer == ' (' and word == ' )' :
36
+ return True
37
+ elif pointer == ' [' and word == ' ]' :
38
+ return True
39
+ elif pointer == ' {' and word == ' }' :
40
+ return True
41
+ else : return False
42
+ ```
You can’t perform that action at this time.
0 commit comments