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; + } +} 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; + } +} 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 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(); + } +}