From 7728a62aa9533979054598524a55afe7ebdec914 Mon Sep 17 00:00:00 2001 From: LSH Date: Sat, 11 May 2024 23:54:06 +0900 Subject: [PATCH] Add week2 --- invert-binary-tree/Seunghyun-Lim.java | 20 ++++++++++++ linked-list-cycle/Seunghyun-Lim.java | 22 +++++++++++++ merge-two-sorted-lists/Seunghyun-Lim.java | 14 +++++++++ reverse-linked-list/Seunghyun-Lim.java | 26 ++++++++++++++++ valid-parentheses/Seunghyun-Lim.java | 38 +++++++++++++++++++++++ 5 files changed, 120 insertions(+) create mode 100644 invert-binary-tree/Seunghyun-Lim.java create mode 100644 linked-list-cycle/Seunghyun-Lim.java create mode 100644 merge-two-sorted-lists/Seunghyun-Lim.java create mode 100644 reverse-linked-list/Seunghyun-Lim.java create mode 100644 valid-parentheses/Seunghyun-Lim.java diff --git a/invert-binary-tree/Seunghyun-Lim.java b/invert-binary-tree/Seunghyun-Lim.java new file mode 100644 index 000000000..6fb9df4e6 --- /dev/null +++ b/invert-binary-tree/Seunghyun-Lim.java @@ -0,0 +1,20 @@ +/** + * 달레님 강의 참고 + * 아직 완벽하게 이해하지 못한 풀이,, 추가로 다시 확인 필요 + * 시간복잡도: O(n) + * 공간복잡도: O(n) + */ +class Solution { + public TreeNode invertTree(TreeNode root) { + if (root == null) return null; + + TreeNode temp = root.left; + root.left = root.right; + root.right = temp; + + invertTree(root.right); + invertTree(root.left); + + return root; + } +} diff --git a/linked-list-cycle/Seunghyun-Lim.java b/linked-list-cycle/Seunghyun-Lim.java new file mode 100644 index 000000000..b5455151f --- /dev/null +++ b/linked-list-cycle/Seunghyun-Lim.java @@ -0,0 +1,22 @@ +/** + * 순환 리스트 + * 달레님 강의 참고 + * 기존에 이해했던 Linked List 처럼 null이 되는 경우가 아니기 때문에 loop에 빠지지 않게 주의해야 한다. + * 시간복잡도: O(n) + * 공간복잡도: O(n) + */ +class Solution { + public boolean hasCycle(ListNode head) { + Set visited = new HashSet<>(); + while (head != null) { + if (visited.contains(head)) { + return true; + } else { + visited.add(head); + head = head.next; + } + } + + return false; + } +} diff --git a/merge-two-sorted-lists/Seunghyun-Lim.java b/merge-two-sorted-lists/Seunghyun-Lim.java new file mode 100644 index 000000000..575f34dc5 --- /dev/null +++ b/merge-two-sorted-lists/Seunghyun-Lim.java @@ -0,0 +1,14 @@ +class Solution { + public ListNode mergeTwoLists(ListNode list1, ListNode list2) { + if (list1 == null) return list2; + if (list2 == null) return list1; + + if (list1.val < list2.val) { + list1.next = mergeTwoLists(list1.next, list2); + return list1; + } else { + list2.next = mergeTwoLists(list1, list2.next); + return list2; + } + } +} diff --git a/reverse-linked-list/Seunghyun-Lim.java b/reverse-linked-list/Seunghyun-Lim.java new file mode 100644 index 000000000..ecfefbf73 --- /dev/null +++ b/reverse-linked-list/Seunghyun-Lim.java @@ -0,0 +1,26 @@ +/** + * 시간복잡도: O(n) + * -> 노드가 존재하지 않을때 까지 반복,, + * 공간복잡도: O(1) + * -> ListNode + */ +class Solution { + public ListNode reverseList(ListNode head) { + if (head == null || head.next == null) { + return head; + } + + ListNode node1 = head; + ListNode node2 = node1.next; + head.next = null; + + while (node1 != null && node2 != null) { + ListNode t = node2.next; + node2.next = node1; + node1 = node2; + node2 = t; + } + + return node1; + } +} diff --git a/valid-parentheses/Seunghyun-Lim.java b/valid-parentheses/Seunghyun-Lim.java new file mode 100644 index 000000000..e3626788b --- /dev/null +++ b/valid-parentheses/Seunghyun-Lim.java @@ -0,0 +1,38 @@ +/** + * 시간복잡도: O(n) + * -> 문자열 s의 길이에 따라 증가 + * 공간복잡도: O(n) + * -> 문자열 s의 길이에 따라 stack의 공간 증가 + */ +class Solution { + public boolean isValid(String s) { + char first = s.charAt(0); + if (s.length() == 1 || first == ')' || first == '}' || first == ']') return false; + + Stack stack = new Stack<>(); + + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + + if (c == '(' || c == '{' || c == '[') { + stack.push(c); + continue; + } + + if (stack.isEmpty()) return false; + + if (c == ')') { + Character pop = stack.pop(); + if (pop != '(') return false; + } else if (c == '}') { + Character pop = stack.pop(); + if (pop != '{') return false; + } else if (c == ']') { + Character pop = stack.pop(); + if (pop != '[') return false; + } + } + + return stack.size() == 0; + } +}