From f989781bbcfe197bac9d87c2e9431ae512a30543 Mon Sep 17 00:00:00 2001 From: chjung99 Date: Sat, 29 Nov 2025 16:39:14 +0900 Subject: [PATCH 1/6] contains-duplicate solution --- contains-duplicate/chjung99.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 contains-duplicate/chjung99.java diff --git a/contains-duplicate/chjung99.java b/contains-duplicate/chjung99.java new file mode 100644 index 0000000000..f69b9d586b --- /dev/null +++ b/contains-duplicate/chjung99.java @@ -0,0 +1,20 @@ +/** + * time: O(n) + * space: O(n) + */ + +import java.util.*; + +class Solution { + public boolean containsDuplicate(int[] nums) { + Set hash = new HashSet<>(); + + for (int num: nums){ + if (hash.contains(num)) return true; + hash.add(num); + } + + return false; + } +} + From e6f65c0ebca64ceab905e199d80cdc9f8d7e0306 Mon Sep 17 00:00:00 2001 From: chjung99 Date: Sat, 29 Nov 2025 16:39:31 +0900 Subject: [PATCH 2/6] invert-binary-tree solution --- invert-binary-tree/chjung99.java | 49 ++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 invert-binary-tree/chjung99.java diff --git a/invert-binary-tree/chjung99.java b/invert-binary-tree/chjung99.java new file mode 100644 index 0000000000..823c48e7d4 --- /dev/null +++ b/invert-binary-tree/chjung99.java @@ -0,0 +1,49 @@ +/** + * time: O(n) + * space: O(n) + */ + +import java.util.*; + +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + public TreeNode invertTree(TreeNode root) { + breadthFirstSearch(root); + return root; + } + public void breadthFirstSearch(TreeNode node){ + Deque queue = new ArrayDeque<>(); + if (node != null){ + queue.add(node); + } + + while (!queue.isEmpty()){ + TreeNode cur = queue.remove(); + + TreeNode tmp = cur.left; + cur.left = cur.right; + cur.right = tmp; + + if (cur.left != null){ + queue.add(cur.left); + } + if (cur.right != null){ + queue.add(cur.right); + } + } + } +} \ No newline at end of file From f9f21fdfec1db8705efadc8a37432e35d9a6ae36 Mon Sep 17 00:00:00 2001 From: chjung99 Date: Sat, 29 Nov 2025 16:39:50 +0900 Subject: [PATCH 3/6] number-of-1-bits solution --- number-of-1-bits/chjung99.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 number-of-1-bits/chjung99.java diff --git a/number-of-1-bits/chjung99.java b/number-of-1-bits/chjung99.java new file mode 100644 index 0000000000..0415d3902a --- /dev/null +++ b/number-of-1-bits/chjung99.java @@ -0,0 +1,15 @@ +/** + * time: O(log n) + * space: O(1) + */ +class Solution { + public int hammingWeight(int n) { + int cnt = 0; + while (n != 0){ + if (n % 2 == 1) cnt ++; + n = (int) (n/2); + } + return cnt; + } +} + From 6e0eb8e1cee5afbed83bd5ab8c5a1ed94dee8398 Mon Sep 17 00:00:00 2001 From: chjung99 Date: Sat, 29 Nov 2025 16:40:05 +0900 Subject: [PATCH 4/6] reverse-linked-list solution --- reverse-linked-list/chjung99.java | 39 +++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 reverse-linked-list/chjung99.java diff --git a/reverse-linked-list/chjung99.java b/reverse-linked-list/chjung99.java new file mode 100644 index 0000000000..b4072d46d1 --- /dev/null +++ b/reverse-linked-list/chjung99.java @@ -0,0 +1,39 @@ +/** + * time: O(n) + * space: O(1) + */ + +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +class Solution { + ListNode tail; + public ListNode reverseList(ListNode head) { + reverseNode(head); + return tail; + } + + public void reverseNode(ListNode node){ + if (node == null) return; + if (node.next == null) { + tail = node; + return; + } + + ListNode nextNode = node.next; + node.next = null; + + reverseNode(nextNode); + + nextNode.next = node; + + } +} + From 8bfa56508b6b2d75636ddafa916aa39e608cb88c Mon Sep 17 00:00:00 2001 From: chjung99 Date: Sat, 29 Nov 2025 16:40:24 +0900 Subject: [PATCH 5/6] valid-anagram solution --- valid-anagram/chjung99.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 valid-anagram/chjung99.java diff --git a/valid-anagram/chjung99.java b/valid-anagram/chjung99.java new file mode 100644 index 0000000000..4f079e8700 --- /dev/null +++ b/valid-anagram/chjung99.java @@ -0,0 +1,24 @@ +/** + * time: O(n) + * space: O(1) + */ + +class Solution { + public boolean isAnagram(String s, String t) { + int[] countS = new int[26]; + int slen = s.length(); + int tlen = t.length(); + + if (slen != tlen) return false; + + for (int i = 0; i < slen; i++){ + countS[s.charAt(i)-'a'] += 1; + countS[t.charAt(i)-'a'] -= 1; + } + for (int i = 0; i < 26; i++){ + if (countS[i] != 0) return false; + } + return true; + } +} + From 4355718d749ba2988fe930c9013c3ed7350e4547 Mon Sep 17 00:00:00 2001 From: chjung99 Date: Sat, 29 Nov 2025 16:41:54 +0900 Subject: [PATCH 6/6] invert-binary-tree solution --- invert-binary-tree/chjung99.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/invert-binary-tree/chjung99.java b/invert-binary-tree/chjung99.java index 823c48e7d4..344a9eae5a 100644 --- a/invert-binary-tree/chjung99.java +++ b/invert-binary-tree/chjung99.java @@ -46,4 +46,5 @@ public void breadthFirstSearch(TreeNode node){ } } } -} \ No newline at end of file +} +