diff --git a/invert-binary-tree/mand2.md b/invert-binary-tree/mand2.md new file mode 100644 index 000000000..5647725d3 --- /dev/null +++ b/invert-binary-tree/mand2.md @@ -0,0 +1,37 @@ +### Intuition +- 재귀호출 + +### Approach +- 재귀 호출 +- 마지막에 left<>right 이동 + +### Complexity +- Time complexity: O(n) +- Space complexity: O(n) + + +# Code + +```python +# Definition for a binary tree node. +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + +class Solution: + def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: + if not root: + return None + + left = self.invertTree(root.left) + right = self.invertTree(root.right) + + root.left = right + root.right = left + + return root + + +``` diff --git a/linked-list-cycle/mand2.md b/linked-list-cycle/mand2.md new file mode 100644 index 000000000..623d177e8 --- /dev/null +++ b/linked-list-cycle/mand2.md @@ -0,0 +1,36 @@ +### Intuition + +### Approach +- pos 는 입력값이 아니다. +- 계속 회전하므로(detour), 언젠가는 one_way 로 가는 값과 만나게 됨. + +### Complexity +- Time complexity: O(n) +- Space complexity: O(1) + + +### Code + +```python +# Definition for singly-linked list. +class ListNode: + def __init__(self, x): + self.val = x + self.next = None + +class Solution: + def hasCycle(self, head: Optional[ListNode]) -> bool: + if not head or not head.next: + return False + + one_way = head + detour = head.next + + while detour and detour.next: + if one_way == detour: + return True + one_way = one_way.next + detour = detour.next.next + + return False +``` diff --git a/merge-two-sorted-lists/mand2.md b/merge-two-sorted-lists/mand2.md new file mode 100644 index 000000000..c78d72939 --- /dev/null +++ b/merge-two-sorted-lists/mand2.md @@ -0,0 +1,41 @@ +### Intuition + +### Approach + +### Complexity +- Time complexity: O(n) +- Space complexity: O(1) + +### Code + +```python +# Definition for singly-linked list. +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + +class Solution: + def mergeTwoLists(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: + if not l1: + return l2 + if not l2: + return l1 + + merged_list = ListNode() + current = merged_list + + while l1 and l2: + if l1.val <= l2.val: + current.next = l1 + l1 = l1.next + else: + current.next = l2 + l2 = l2.next + + current = current.next # pointer setting. + + current.next = l1 if l1 else l2 + + return merged_list.next +``` diff --git a/reverse-linked-list/mand2.md b/reverse-linked-list/mand2.md new file mode 100644 index 000000000..1a56fe2e7 --- /dev/null +++ b/reverse-linked-list/mand2.md @@ -0,0 +1,37 @@ +### Intuition +- 투 포인터 (prev, curr) + +### Approach +- 투 포인터 +- 포인터를 이동하기 전에 prev 가 가리키는 (.next) 를 reverse 해 주어야 한다. + +### Complexity +- Time complexity: O(n) +- Space complexity: O(1) + + +### Code + +```python +# Definition for singly-linked list. +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + +class Solution: + def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: + if not head: + return head + + prev, curr = None, head + + while curr: + next_node = curr.next + curr.next = prev + prev = curr + curr = next_node + + return prev + +``` diff --git a/valid-parentheses/mand2.md b/valid-parentheses/mand2.md new file mode 100644 index 000000000..0046e8c75 --- /dev/null +++ b/valid-parentheses/mand2.md @@ -0,0 +1,42 @@ +### Intuition +- opening(`([{`) 스택을 쌓는다. +- 비교한다. + +### Approach +1. 먼저 입력받은 글자의 길이가 짝수가 아니면 무조건 **False**. +2. for 문 + - opening(`([{`) 이면 stack 에 넣는다. + - stack 이 비어있거나, 짝이 맞지 않으면(`is_parentheses()==False`) **False**. +3. 다 완료되고 나서, 스택이 비어있다면 **True**. + + +### Complexity +- Time complexity: O(n) +- Space complexity: O(n) + + +### Code + +```python +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + if len(s) % 2 != 0: + return False + for word in s: + if word in '([{': + stack.append(word) + elif not stack or is_parentheses(stack.pop(), word) is False: + return False + return not stack + + +def is_parentheses(pointer, word) -> bool: + if pointer == '(' and word == ')': + return True + elif pointer == '[' and word == ']': + return True + elif pointer == '{' and word == '}': + return True + else: return False +```