diff --git a/climbing-stairs/chjung99.java b/climbing-stairs/chjung99.java new file mode 100644 index 0000000000..339b84994f --- /dev/null +++ b/climbing-stairs/chjung99.java @@ -0,0 +1,15 @@ +class Solution { + public int climbStairs(int n) { + int[] dp = new int[46]; + dp[0] = 1; + dp[1] = 1; + dp[2] = dp[1] + dp[0]; + dp[3] = dp[2] + dp[1]; + + for (int i = 4; i <= n; i++) { + dp[i] = dp[i-1] + dp[i-2]; + } + return dp[n]; + } +} + diff --git a/merge-two-sorted-lists/chjung99.java b/merge-two-sorted-lists/chjung99.java new file mode 100644 index 0000000000..1820aff51c --- /dev/null +++ b/merge-two-sorted-lists/chjung99.java @@ -0,0 +1,46 @@ +/** + * 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 { + public ListNode mergeTwoLists(ListNode list1, ListNode list2) { + ListNode head = initNode(list1, list2); + ListNode ptr = head; + + while (true){ + if (list1 != null && list2 != null) { + if (list1.val < list2.val) { + ptr.val = list1.val; + list1 = list1.next; + } else { + ptr.val = list2.val; + list2 = list2.next; + } + } + else if (list1 == null && list2 != null) { + ptr.val = list2.val; + list2 = list2.next; + } + else if (list1 != null && list2 == null) { + ptr.val = list1.val; + list1 = list1.next; + } + if (list1 == null && list2 == null) break; + ptr.next = new ListNode(); + ptr = ptr.next; + } + return head; + } + + public ListNode initNode(ListNode list1, ListNode list2) { + if (list1 == null && list2 == null) return null; + return new ListNode(); + } +} + diff --git a/same-tree/chjung99.java b/same-tree/chjung99.java new file mode 100644 index 0000000000..e411431f13 --- /dev/null +++ b/same-tree/chjung99.java @@ -0,0 +1,48 @@ +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 { + final int NULL_MARKER = Integer.MIN_VALUE; + public boolean isSameTree(TreeNode p, TreeNode q) { + Queue OrderP = new ArrayDeque<>(); + Queue OrderQ = new ArrayDeque<>(); + trevasalTree(p, OrderP); + trevasalTree(q, OrderQ); + + if (OrderP.size() != OrderQ.size()) return false; + + while (!OrderP.isEmpty() && !OrderQ.isEmpty()){ + Integer a = OrderP.poll(); + Integer b = OrderQ.poll(); + if (!a.equals(b)) return false; + } + + return OrderP.isEmpty() && OrderQ.isEmpty(); + } + public void trevasalTree(TreeNode t, Queue queue){ + // System.out.print(t.val+ " "); + if (t == null){ + queue.add(NULL_MARKER); + return; + } + queue.add(t.val); + trevasalTree(t.left, queue); + trevasalTree(t.right, queue); + + } +} + diff --git a/two-sum/chjung99.java b/two-sum/chjung99.java new file mode 100644 index 0000000000..265ca7a1a3 --- /dev/null +++ b/two-sum/chjung99.java @@ -0,0 +1,25 @@ +import java.util.*; + +class Solution { + public int[] twoSum(int[] nums, int target) { + HashMap> map = new HashMap(); + for (int i = 0; i < nums.length; i++) { + if (!map.containsKey(nums[i])) { + map.put(nums[i], new ArrayList()); + } + map.get(nums[i]).add(i); + } + + for (Map.Entry> e: map.entrySet()) { + int other = target - e.getKey(); + if (map.containsKey(other)) { + if (e.getKey() == other && map.get(other).size() > 1) { + return new int[]{e.getValue().get(0), e.getValue().get(1)}; + } + return new int[]{e.getValue().get(0), map.get(other).get(0)}; + } + } + return new int[]{}; + } +} + diff --git a/valid-parentheses/chjung99.java b/valid-parentheses/chjung99.java new file mode 100644 index 0000000000..d63793478a --- /dev/null +++ b/valid-parentheses/chjung99.java @@ -0,0 +1,32 @@ +import java.util.*; + +class Solution { + public boolean isValid(String s) { + ArrayDeque stack = new ArrayDeque<>(); + for (int i = 0; i < s.length(); i++){ + if (s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '[' ){ + stack.push(s.charAt(i)); + continue; + } + + if (s.charAt(i) == ')' && !stack.isEmpty() && stack.peek() == '('){ + stack.pop(); + continue; + } + + if (s.charAt(i) == '}' && !stack.isEmpty() && stack.peek() == '{'){ + stack.pop(); + continue; + } + + if (s.charAt(i) == ']' && !stack.isEmpty() && stack.peek() == '['){ + stack.pop(); + continue; + } + return false; + } + return stack.isEmpty(); + + } +} +