From 17b7837cc4493b19963ca213764b86c12bf73258 Mon Sep 17 00:00:00 2001 From: bky373 Date: Thu, 9 May 2024 00:39:40 +0900 Subject: [PATCH 1/5] [week2] solve 20. Valid Parentheses --- valid-parentheses/bky373.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 valid-parentheses/bky373.java diff --git a/valid-parentheses/bky373.java b/valid-parentheses/bky373.java new file mode 100644 index 000000000..d0749c25a --- /dev/null +++ b/valid-parentheses/bky373.java @@ -0,0 +1,29 @@ +/** + * - 문제: https://leetcode.com/problems/valid-parentheses/ + * - TC: O(N) + * - SC: O(1) + */ +class Solution_20 { + + public boolean isValid(String s) { + Stack st = new Stack<>(); + Map pairs = Map.of('(', ')', '{', '}', '[', ']'); + + for (int i = 0; i < s.length(); i++) { + // 열린 괄호이면 스택에 push + if (pairs.containsKey(s.charAt(i))) { + st.push(s.charAt(i)); + } else { // 닫힌 괄호인데 + if (st.isEmpty()) { // 스택이 비어있으면(열린 괄호가 없으면) false 반환 + return false; + } + // 스택의 최근 요소(열린 괄호)와 짝을 이루지 않으면 false 반환 + if (s.charAt(i) != pairs.get(st.pop())) { + return false; + } + } + } + // 열린 괄호가 남아 있을 수 있으므로 목록이 비어있는지 체크 + return st.isEmpty(); + } +} From 7e22b46874977cd78d88aaea97f83138f20fb219 Mon Sep 17 00:00:00 2001 From: bky373 Date: Thu, 9 May 2024 00:47:12 +0900 Subject: [PATCH 2/5] [week2] solve 206. Reverse Linked List --- reverse-linked-list/bky373.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 reverse-linked-list/bky373.java diff --git a/reverse-linked-list/bky373.java b/reverse-linked-list/bky373.java new file mode 100644 index 000000000..65438d058 --- /dev/null +++ b/reverse-linked-list/bky373.java @@ -0,0 +1,21 @@ +/** + * https://leetcode.com/problems/reverse-linked-list/ + * TC: O(N) + * SC: O(N) + */ +class Solution_206 { + + public ListNode reverseList(ListNode head) { + if (head == null) { + return head; + } + ListNode a = new ListNode(head.val); + ListNode b; + while (head.next != null) { + b = new ListNode(head.next.val, a); + a = b; + head = head.next; + } + return a; + } +} From fdc0c0ab52e56ba47c95803601fd7280c8770909 Mon Sep 17 00:00:00 2001 From: bky373 Date: Fri, 10 May 2024 00:50:56 +0900 Subject: [PATCH 3/5] [week2] solve 226. Invert Binary Tree --- invert-binary-tree/bky373.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 invert-binary-tree/bky373.java diff --git a/invert-binary-tree/bky373.java b/invert-binary-tree/bky373.java new file mode 100644 index 000000000..5813797f1 --- /dev/null +++ b/invert-binary-tree/bky373.java @@ -0,0 +1,16 @@ +/** + * - 문제: https://leetcode.com/problems/linked-list-cycle/ + * - TC: O(N) + * - SC: O(N) + */ +public class Solution_226 { + public TreeNode invertTree(TreeNode root) { + if (root == null) { + return null; + } + TreeNode tmp = root.left; + root.left = invertTree(root.right); + root.right = invertTree(tmp); + return root; + } +} From 24af36f1ffa66e4edf98d3957f5fb3226ef796d6 Mon Sep 17 00:00:00 2001 From: bky373 Date: Fri, 10 May 2024 00:52:59 +0900 Subject: [PATCH 4/5] [week2] solve 21. Merge Two Sorted Lists --- merge-two-sorted-lists/bky373.java | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 merge-two-sorted-lists/bky373.java diff --git a/merge-two-sorted-lists/bky373.java b/merge-two-sorted-lists/bky373.java new file mode 100644 index 000000000..0288f4c86 --- /dev/null +++ b/merge-two-sorted-lists/bky373.java @@ -0,0 +1,33 @@ +/** + * TC: O(N) + * SC: O(N) + */ +class Solution_21 { + public ListNode mergeTwoLists(ListNode list1, ListNode list2) { + if (list1 == null && list2 == null) { + return null; + } + + List nums = new ArrayList<>(); + + while (list1 != null) { + nums.add(list1.val); + list1 = list1.next; + } + while (list2 != null) { + nums.add(list2.val); + list2 = list2.next; + } + + Object[] arr = nums.toArray(); + Arrays.sort(arr); + + ListNode head = new ListNode((int) arr[0]); + ListNode curr = head; + for (int i=1; i Date: Fri, 10 May 2024 19:01:22 +0900 Subject: [PATCH 5/5] [week2] solve 141. Linked List Cycle --- linked-list-cycle/bky373.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 linked-list-cycle/bky373.java diff --git a/linked-list-cycle/bky373.java b/linked-list-cycle/bky373.java new file mode 100644 index 000000000..6bd0b0b68 --- /dev/null +++ b/linked-list-cycle/bky373.java @@ -0,0 +1,22 @@ +/** + * - 문제: https://leetcode.com/problems/linked-list-cycle/ + * - TC: O(N) + * - SC: O(1) + */ +public class Solution { + public boolean hasCycle(ListNode head) { + if (head == null || head.next == null) { + return false; + } + int min = -10001; + + while (head.next != null) { + if (head.next.val == min) { + return true; + } + head.val = min; + head = head.next; + } + return false; + } +}