From c7be66e78312f8e6c255092794e2b2d09ba53303 Mon Sep 17 00:00:00 2001 From: zsong96wisc <61021277+zsong96wisc@users.noreply.github.com> Date: Wed, 27 Aug 2025 14:13:32 -0500 Subject: [PATCH] My own solution to large problem categories My own solution to linkedlist, Array, Binary Tree Problem --- problems/100_Same_Tree/solution_1.txt | 26 ++++++++ problems/101_Symmetric_Tree/solution_1.txt | 29 +++++++++ .../solution_1.txt | 22 +++++++ .../solution_1.txt | 37 ++++++++++++ problems/112_Path_Sum/solution_1.txt | 29 +++++++++ problems/113_Path_Sum_II/solution_1.txt | 39 ++++++++++++ .../solution_1.txt | 18 ++++++ .../147_Insertion_Sort_List/solution_1.txt | 47 +++++++++++++++ problems/148_Sort_List/solution_1.txt | 60 +++++++++++++++++++ problems/15_3sum/solution_1.txt | 32 ++++++++++ problems/16_3sum_Closest/solution_1.txt | 25 ++++++++ .../206_Reverse_Linked_List/solution_1.txt | 27 +++++++++ .../206_Reverse_Linked_List/solution_2.txt | 19 ++++++ .../21_Merge_Two_Sorted_Lists/solution_1.txt | 30 ++++++++++ .../226_Invert_Binary_Tree/solution_1.txt | 25 ++++++++ .../240_Search_a_2D_Matrix_II/solution_1.txt | 12 ++++ problems/27_Remove_Element/Solution 1.txt | 16 +++++ problems/27_Remove_Element/Solution 2.txt | 17 ++++++ problems/28_Implement_strStr()/solution_1.txt | 18 ++++++ problems/28_Implement_strStr()/solution_2.txt | 57 ++++++++++++++++++ .../328_Odd_Even_Linked_List/solution_1.txt | 28 +++++++++ .../Solution_1.txt | 25 ++++++++ .../solution_1.txt | 43 +++++++++++++ problems/61_Rotate_List/solution_1.txt | 38 ++++++++++++ problems/61_Rotate_List/solution_2.txt | 42 +++++++++++++ problems/69_Sqrt(x)/solution_1.txt | 20 +++++++ problems/74_Search_a_2D_Matrix/solution_1.txt | 36 +++++++++++ problems/75_Sort_Colors/solution_1.txt | 20 +++++++ .../solution_1.txt | 32 ++++++++++ .../solution_1.txt | 27 +++++++++ .../solution_1.txt | 29 +++++++++ .../solution_2.txt | 39 ++++++++++++ 32 files changed, 964 insertions(+) create mode 100644 problems/100_Same_Tree/solution_1.txt create mode 100644 problems/101_Symmetric_Tree/solution_1.txt create mode 100644 problems/104_Maximum_Depth_of_Binary_Tree/solution_1.txt create mode 100644 problems/108_Convert_Sorted_Array_to_Binary_Search_Tree/solution_1.txt create mode 100644 problems/112_Path_Sum/solution_1.txt create mode 100644 problems/113_Path_Sum_II/solution_1.txt create mode 100644 problems/11_Container With Most Water/solution_1.txt create mode 100644 problems/147_Insertion_Sort_List/solution_1.txt create mode 100644 problems/148_Sort_List/solution_1.txt create mode 100644 problems/15_3sum/solution_1.txt create mode 100644 problems/16_3sum_Closest/solution_1.txt create mode 100644 problems/206_Reverse_Linked_List/solution_1.txt create mode 100644 problems/206_Reverse_Linked_List/solution_2.txt create mode 100644 problems/21_Merge_Two_Sorted_Lists/solution_1.txt create mode 100644 problems/226_Invert_Binary_Tree/solution_1.txt create mode 100644 problems/240_Search_a_2D_Matrix_II/solution_1.txt create mode 100644 problems/27_Remove_Element/Solution 1.txt create mode 100644 problems/27_Remove_Element/Solution 2.txt create mode 100644 problems/28_Implement_strStr()/solution_1.txt create mode 100644 problems/28_Implement_strStr()/solution_2.txt create mode 100644 problems/328_Odd_Even_Linked_List/solution_1.txt create mode 100644 problems/33_Search_in_Rotated_Sorted_Array/Solution_1.txt create mode 100644 problems/34_Find_First_and_Last_Position_of_Element_in_Sorted_Array/solution_1.txt create mode 100644 problems/61_Rotate_List/solution_1.txt create mode 100644 problems/61_Rotate_List/solution_2.txt create mode 100644 problems/69_Sqrt(x)/solution_1.txt create mode 100644 problems/74_Search_a_2D_Matrix/solution_1.txt create mode 100644 problems/75_Sort_Colors/solution_1.txt create mode 100644 problems/83_Remove_Duplicates_from_Sorted_List/solution_1.txt create mode 100644 problems/94_Binary_Tree_Inorder_Traversal/solution_1.txt create mode 100644 problems/98_Validate_Binary_Search_Tree/solution_1.txt create mode 100644 problems/98_Validate_Binary_Search_Tree/solution_2.txt diff --git a/problems/100_Same_Tree/solution_1.txt b/problems/100_Same_Tree/solution_1.txt new file mode 100644 index 0000000000..44f9b4ed0f --- /dev/null +++ b/problems/100_Same_Tree/solution_1.txt @@ -0,0 +1,26 @@ +/** + * 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 boolean isSameTree(TreeNode p, TreeNode q) { + if (p == q && q == null) return true; + + if ((p==null && q!=null) || (q==null && p!=null)) return false; + + if (p.val != q.val) return false; + + return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); + } +} \ No newline at end of file diff --git a/problems/101_Symmetric_Tree/solution_1.txt b/problems/101_Symmetric_Tree/solution_1.txt new file mode 100644 index 0000000000..4c47924c23 --- /dev/null +++ b/problems/101_Symmetric_Tree/solution_1.txt @@ -0,0 +1,29 @@ +/** + * 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 boolean isSymmetric(TreeNode root) { + return checkSymmetric(root, root); + } + + private boolean checkSymmetric(TreeNode root1, TreeNode root2){ + if (root1 == null && root2 == null) { + return true; + } else if (root1 == null || root2 == null){ + return false; + } + return (root1.val == root2.val) && checkSymmetric(root1.left, root2.right) && checkSymmetric(root1.right, root2.left); + } +} \ No newline at end of file diff --git a/problems/104_Maximum_Depth_of_Binary_Tree/solution_1.txt b/problems/104_Maximum_Depth_of_Binary_Tree/solution_1.txt new file mode 100644 index 0000000000..c395e4d821 --- /dev/null +++ b/problems/104_Maximum_Depth_of_Binary_Tree/solution_1.txt @@ -0,0 +1,22 @@ +/** + * 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 int maxDepth(TreeNode root) { + if (root == null) return 0; + + return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); + } +} \ No newline at end of file diff --git a/problems/108_Convert_Sorted_Array_to_Binary_Search_Tree/solution_1.txt b/problems/108_Convert_Sorted_Array_to_Binary_Search_Tree/solution_1.txt new file mode 100644 index 0000000000..ba699d2537 --- /dev/null +++ b/problems/108_Convert_Sorted_Array_to_Binary_Search_Tree/solution_1.txt @@ -0,0 +1,37 @@ +/** + * 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 sortedArrayToBST(int[] nums) { + return split(0, nums.length - 1, nums); + } + + private TreeNode split(int left, int right, int[] nums){ + if (right - left == 0){ + return new TreeNode(nums[left]); + } + + if (right - left == 1){ + return new TreeNode(nums[left], null, new TreeNode(nums[right])); + } + + int mid = left + (right - left) / 2; + TreeNode root = new TreeNode(nums[mid]); + root.left = split(left, mid - 1, nums); + root.right = split(mid + 1, right, nums); + + return root; + } +} \ No newline at end of file diff --git a/problems/112_Path_Sum/solution_1.txt b/problems/112_Path_Sum/solution_1.txt new file mode 100644 index 0000000000..5ae9fbe782 --- /dev/null +++ b/problems/112_Path_Sum/solution_1.txt @@ -0,0 +1,29 @@ +/** + * 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 boolean hasPathSum(TreeNode root, int targetSum) { + return Sum(root, targetSum); + } + + public boolean Sum(TreeNode node, int targetSum) { + if (node == null) return false; + if (node.left == null && node.right == null + && targetSum == node.val) return true; + + return Sum(node.left, targetSum - node.val) + || Sum (node.right, targetSum - node.val); + } +} \ No newline at end of file diff --git a/problems/113_Path_Sum_II/solution_1.txt b/problems/113_Path_Sum_II/solution_1.txt new file mode 100644 index 0000000000..8eb55e57c1 --- /dev/null +++ b/problems/113_Path_Sum_II/solution_1.txt @@ -0,0 +1,39 @@ +/** + * 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 List> pathSum(TreeNode root, int targetSum) { + List> result = new ArrayList<>(); + Sum(root, targetSum, result, new ArrayList()); + return result; + } + + private void Sum(TreeNode node, int targetSum, List> result, ArrayList lists){ + if (node == null) return; + if (node.left == null && node.right == null && targetSum == node.val) { + lists.add(node.val); + result.add(new ArrayList(lists)); + lists.remove(lists.size() - 1); + return; + } + + lists.add(node.val); + + Sum(node.left, targetSum - node.val, result, lists); + Sum(node.right, targetSum - node.val, result, lists); + + lists.remove(lists.size() - 1); + } +} \ No newline at end of file diff --git a/problems/11_Container With Most Water/solution_1.txt b/problems/11_Container With Most Water/solution_1.txt new file mode 100644 index 0000000000..f5fdf09459 --- /dev/null +++ b/problems/11_Container With Most Water/solution_1.txt @@ -0,0 +1,18 @@ +class Solution { + public int maxArea(int[] height) { + int start = 0; + int end = height.length - 1; + int max = 0; + + while (start < end){ + max = Math.max((end - start) * Math.min(height[start], height[end]), max); + + if (height[start] > height[end]) { + end--; + } else { + start++; + } + } + return max; + } +} \ No newline at end of file diff --git a/problems/147_Insertion_Sort_List/solution_1.txt b/problems/147_Insertion_Sort_List/solution_1.txt new file mode 100644 index 0000000000..70804e2bea --- /dev/null +++ b/problems/147_Insertion_Sort_List/solution_1.txt @@ -0,0 +1,47 @@ +/** + * 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 insertionSortList(ListNode head) { + if (head == null || head.next == null) return head; + + ListNode current = head; + + while (current != null && current.next != null){ + if (current.val > current.next.val){ + ListNode value = current.next; + current.next = current.next.next; + + if (head.val >= value.val){ + value.next = head; + head = value; + continue; + } + + ListNode m = head; + + while (m.next != null){ + if (m.next.val > value.val){ + value.next = m.next; + m.next = value; + break; + } + m = m.next; + } + } else { + current = current.next; + } + + } + + + return head; + } +} \ No newline at end of file diff --git a/problems/148_Sort_List/solution_1.txt b/problems/148_Sort_List/solution_1.txt new file mode 100644 index 0000000000..e44b743340 --- /dev/null +++ b/problems/148_Sort_List/solution_1.txt @@ -0,0 +1,60 @@ +/** + * 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 sortList(ListNode head) { + if (head == null || head.next == null) return head; + ListNode mid = findMid(head); + ListNode left = sortList(head); + ListNode right = sortList(mid); + return MergeList(left, right); + } + + private ListNode MergeList (ListNode L1, ListNode L2){ + ListNode dummyNode = new ListNode(); + ListNode tail = dummyNode; + + while (L1 != null && L2 != null){ + if (L1.val <= L2.val){ + tail.next = L1; + L1 = L1.next; + tail = tail.next; + } else { + tail.next = L2; + L2 = L2.next; + tail = tail.next; + } + } + + if (L1 == null) { + tail.next = L2; + } else { + tail.next = L1; + } + return dummyNode.next; + } + + private ListNode findMid(ListNode head){ + ListNode slow = null; + + while (head != null && head.next != null){ + if (slow == null){ + slow = head; + } else { + slow = slow.next; + } + head = head.next.next; + } + + ListNode mid = slow.next; + slow.next = null; + return mid; + } +} \ No newline at end of file diff --git a/problems/15_3sum/solution_1.txt b/problems/15_3sum/solution_1.txt new file mode 100644 index 0000000000..60b246ffc5 --- /dev/null +++ b/problems/15_3sum/solution_1.txt @@ -0,0 +1,32 @@ +class Solution { + public List> threeSum(int[] nums) { + Arrays.sort(nums); + List> result = new ArrayList<>(); + int start = 0; + int end = 0; + + for (int i = 0; i < nums.length-2; i++){ + if (i > 0 && nums[i] == nums[i-1]) + continue; + int j = i + 1; + int k = nums.length-1; + while(j < k){ + if (nums[j] + nums[k] > -nums[i]) + k--; + else if (nums[j] + nums[k] < -nums[i]) + j++; + else { + ArrayList numbers = new ArrayList<>(); + numbers.add(nums[i]); + numbers.add(nums[j]); + numbers.add(nums[k]); + result.add(numbers); + j++; + while (nums[j] == nums[j-1] && j < k) + j++; + } + } + } + return result; + } +} \ No newline at end of file diff --git a/problems/16_3sum_Closest/solution_1.txt b/problems/16_3sum_Closest/solution_1.txt new file mode 100644 index 0000000000..b32faed36f --- /dev/null +++ b/problems/16_3sum_Closest/solution_1.txt @@ -0,0 +1,25 @@ +class Solution { + public int threeSumClosest(int[] nums, int target) { + Arrays.sort(nums); + + int diff = Integer.MAX_VALUE; + + for (int i = 0; i < nums.length - 2; i++){ + int j = i + 1; + int k = nums.length - 1; + while (j < k){ + if (Math.abs(nums[i] + nums[j] + nums[k] - target) < Math.abs(diff)){ + diff = nums[i] + nums[j] + nums[k] - target; + } + if (nums[i] + nums[j] + nums[k] > target){ + k--; + } else if (nums[i] + nums[j] + nums[k] < target){ + j++; + } else { + return nums[i] + nums[j] + nums[k]; + } + } + } + return diff + target; + } +} \ No newline at end of file diff --git a/problems/206_Reverse_Linked_List/solution_1.txt b/problems/206_Reverse_Linked_List/solution_1.txt new file mode 100644 index 0000000000..582bf0e4b8 --- /dev/null +++ b/problems/206_Reverse_Linked_List/solution_1.txt @@ -0,0 +1,27 @@ +/** + * 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 reverseList(ListNode head) { + if (head == null || head.next == null) return head; + ListNode pre = head; + ListNode tail = head.next; + head.next = null; + + while (tail != null){ + ListNode next = tail.next; + tail.next = pre; + pre = tail; + tail = next; + } + + return pre; + } +} \ No newline at end of file diff --git a/problems/206_Reverse_Linked_List/solution_2.txt b/problems/206_Reverse_Linked_List/solution_2.txt new file mode 100644 index 0000000000..e8f0384aa6 --- /dev/null +++ b/problems/206_Reverse_Linked_List/solution_2.txt @@ -0,0 +1,19 @@ +/** + * 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 reverseList(ListNode head) { + if (head == null || head.next == null) return head; + ListNode p = reverseList(head.next); + head.next.next = head; + head.next = null; + return p; + } +} \ No newline at end of file diff --git a/problems/21_Merge_Two_Sorted_Lists/solution_1.txt b/problems/21_Merge_Two_Sorted_Lists/solution_1.txt new file mode 100644 index 0000000000..cf28fcf983 --- /dev/null +++ b/problems/21_Merge_Two_Sorted_Lists/solution_1.txt @@ -0,0 +1,30 @@ +/** + * 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 l1, ListNode l2) { + ListNode head = new ListNode(); + ListNode tail = head; + + while (l1 != null || l2 != null){ + if (l2 == null || (l1 != null && l1.val <= l2.val)){ + tail.next = l1; + tail = l1; + l1 = l1.next; + } else { + tail.next = l2; + tail = l2; + l2 = l2.next; + } + } + + return head.next; + } +} \ No newline at end of file diff --git a/problems/226_Invert_Binary_Tree/solution_1.txt b/problems/226_Invert_Binary_Tree/solution_1.txt new file mode 100644 index 0000000000..fcaa5a4938 --- /dev/null +++ b/problems/226_Invert_Binary_Tree/solution_1.txt @@ -0,0 +1,25 @@ +/** + * 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) { + if (root == null) return null; + TreeNode left = root.left; + root.left = invertTree(root.right); + root.right = invertTree(left); + return root; + } + +} \ No newline at end of file diff --git a/problems/240_Search_a_2D_Matrix_II/solution_1.txt b/problems/240_Search_a_2D_Matrix_II/solution_1.txt new file mode 100644 index 0000000000..1a6b395f79 --- /dev/null +++ b/problems/240_Search_a_2D_Matrix_II/solution_1.txt @@ -0,0 +1,12 @@ +class Solution { + public boolean searchMatrix(int[][] matrix, int target) { + int row = matrix.length - 1; int col = 0; + + while (row >= 0 && col < matrix[0].length){ + if (matrix[row][col] == target) return true; + else if (matrix[row][col] > target) row--; + else col++; + } + return false; + } +} \ No newline at end of file diff --git a/problems/27_Remove_Element/Solution 1.txt b/problems/27_Remove_Element/Solution 1.txt new file mode 100644 index 0000000000..8ff31bb63e --- /dev/null +++ b/problems/27_Remove_Element/Solution 1.txt @@ -0,0 +1,16 @@ +class Solution { + public int removeElement(int[] nums, int val) { + int first = 0; + int second = 0; + + while (second < nums.length){ + if (nums[second] != val){ + nums[first] = nums[second]; + first++; + } + second++; + } + + return first; + } +} \ No newline at end of file diff --git a/problems/27_Remove_Element/Solution 2.txt b/problems/27_Remove_Element/Solution 2.txt new file mode 100644 index 0000000000..f09e56e27e --- /dev/null +++ b/problems/27_Remove_Element/Solution 2.txt @@ -0,0 +1,17 @@ +class Solution { + public int removeElement(int[] nums, int val) { + if(nums.length == 0) return 0; + int first = 0; + int last = nums.length; + + while (first != last){ + if (nums[first] == val){ + nums[first] = nums[last - 1]; + last--; + } else + first++; + + } + return last; + } +} \ No newline at end of file diff --git a/problems/28_Implement_strStr()/solution_1.txt b/problems/28_Implement_strStr()/solution_1.txt new file mode 100644 index 0000000000..823a316794 --- /dev/null +++ b/problems/28_Implement_strStr()/solution_1.txt @@ -0,0 +1,18 @@ +class Solution { + public int strStr(String haystack, String needle) { + if(needle.length() == 0) return 0; + if(haystack.length() < needle.length()) return -1; + + for (int start = 0; start <= haystack.length() - needle.length(); ++start){ + int end = 0; + for (end = 0; end < needle.length(); ++end){ + if (haystack.charAt(start+end) != needle.charAt(end)){ + break; + } + } + if (end == needle.length()) return start; + } + return -1; + + } +} \ No newline at end of file diff --git a/problems/28_Implement_strStr()/solution_2.txt b/problems/28_Implement_strStr()/solution_2.txt new file mode 100644 index 0000000000..e09d126e69 --- /dev/null +++ b/problems/28_Implement_strStr()/solution_2.txt @@ -0,0 +1,57 @@ +class Solution { + public int strStr(String haystack, String needle) { + int m = haystack.length(); + int n = needle.length(); + if (n == 0) return 0; + int[] prefix = prefix_table(needle.toCharArray()); + + // haystack[i] length = m + // needle[j] length = n + + + int i = 0; + int j = 0; + while (i < m) { + if (j == n-1 && haystack.charAt(i) == needle.charAt(j)){ + return i - j; + } + + if (haystack.charAt(i) == needle.charAt(j)){ + i++; j++; + } else { + j = prefix[j]; + if (j == -1){ + i++; j++; + } + } + } + + return -1; + } + + private int[] prefix_table(char[] pattern){ + int[] prefix = new int[pattern.length]; + prefix[0] = -1; + if (pattern.length > 1) + prefix[1] = 0; + + int len = 0; + int i = 1; + + while (i < pattern.length - 1){ + if (pattern[len] == pattern[i]){ + prefix[i + 1] = ++len; + i++; + } else { + if (len > 0){ + len = prefix[len]; + } else { + prefix[i + 1] = len; + i++; + } + } + } + + return prefix; + } +} \ No newline at end of file diff --git a/problems/328_Odd_Even_Linked_List/solution_1.txt b/problems/328_Odd_Even_Linked_List/solution_1.txt new file mode 100644 index 0000000000..9e5d947efb --- /dev/null +++ b/problems/328_Odd_Even_Linked_List/solution_1.txt @@ -0,0 +1,28 @@ +/** + * 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 oddEvenList(ListNode head) { + if (head == null) return head; + ListNode evenhead = head.next; + ListNode odd = head; + ListNode even = head.next; + + while (even != null && even.next != null){ + odd.next = even.next; + odd = odd.next; + even.next = odd.next; + even = even.next; + } + + odd.next = evenhead; + return head; + } +} \ No newline at end of file diff --git a/problems/33_Search_in_Rotated_Sorted_Array/Solution_1.txt b/problems/33_Search_in_Rotated_Sorted_Array/Solution_1.txt new file mode 100644 index 0000000000..39dc8fa7dc --- /dev/null +++ b/problems/33_Search_in_Rotated_Sorted_Array/Solution_1.txt @@ -0,0 +1,25 @@ +class Solution { + public int search(int[] nums, int target) { + int left = 0; + int right = nums.length - 1; + + while (left <= right){ + int mid = left + (right - left) / 2; + if (nums[mid] == target) return mid; + if (nums[mid] > nums[right]){ + if (target >= nums[left] && target < nums[mid]){ + right = mid - 1; + } else { + left = mid + 1; + } + } else { + if (target > nums[mid] && target <= nums[right]){ + left = mid + 1; + } else { + right = mid - 1; + } + } + } + return -1; + } +} \ No newline at end of file diff --git a/problems/34_Find_First_and_Last_Position_of_Element_in_Sorted_Array/solution_1.txt b/problems/34_Find_First_and_Last_Position_of_Element_in_Sorted_Array/solution_1.txt new file mode 100644 index 0000000000..5cb94b22e4 --- /dev/null +++ b/problems/34_Find_First_and_Last_Position_of_Element_in_Sorted_Array/solution_1.txt @@ -0,0 +1,43 @@ +class Solution { + public int[] searchRange(int[] nums, int target) { + if (nums.length == 0) return new int[]{-1, -1}; + + return new int[]{leftmost(nums, target), rightmost(nums, target)}; + } + + private int leftmost(int[] nums, int target){ + int left = 0; int right = nums.length - 1; int index = -1; + + while (left <= right){ + int mid = left + (right - left) / 2; + if (nums[mid] == target){ + index = mid; + right = mid - 1; + } + else if (nums[mid] > target){ + right = mid - 1; + } else { + left = mid + 1; + } + } + return index; + } + + private int rightmost(int[] nums, int target){ + int left = 0; int right = nums.length - 1; int index = -1; + + while (left <= right){ + int mid = left + (right - left) / 2; + if (nums[mid] == target){ + index = mid; + left = mid + 1; + } + else if (nums[mid] > target){ + right = mid - 1; + } else { + left = mid + 1; + } + } + return index; + } +} \ No newline at end of file diff --git a/problems/61_Rotate_List/solution_1.txt b/problems/61_Rotate_List/solution_1.txt new file mode 100644 index 0000000000..06066ac339 --- /dev/null +++ b/problems/61_Rotate_List/solution_1.txt @@ -0,0 +1,38 @@ +/** + * 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 rotateRight(ListNode head, int k) { + if (head == null || k == 0 || head.next == null) return head; + + ListNode count = head; + ListNode pre = head; + ListNode current = head; + int counter = 1; + + while (count.next != null){ + counter++; + count = count.next; + } + + k = k % counter; + + if (k == 0) return head; + else { + count.next = head; + for (int i = 1; i < counter - k; i++){ + pre = pre.next; + } + current = pre.next; + pre.next = null; + } + return current; + } +} \ No newline at end of file diff --git a/problems/61_Rotate_List/solution_2.txt b/problems/61_Rotate_List/solution_2.txt new file mode 100644 index 0000000000..7731aec0a3 --- /dev/null +++ b/problems/61_Rotate_List/solution_2.txt @@ -0,0 +1,42 @@ +/** + * 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 rotateRight(ListNode head, int k) { + if (head == null || k == 0 || head.next == null) return head; + + ListNode count = head; + ListNode fast = head; + ListNode slow = head; + int counter = 1; + + while (count.next != null){ + counter++; + count = count.next; + } + + k = k % counter; + + for (int i = 0; i < k; i++){ + fast = fast.next; + } + + if (fast == null) return head; + while (fast.next != null){ + fast = fast.next; + slow = slow.next; + } + + fast.next = head; + fast = slow.next; + slow.next = null; + return fast; + } +} \ No newline at end of file diff --git a/problems/69_Sqrt(x)/solution_1.txt b/problems/69_Sqrt(x)/solution_1.txt new file mode 100644 index 0000000000..95494bd626 --- /dev/null +++ b/problems/69_Sqrt(x)/solution_1.txt @@ -0,0 +1,20 @@ +class Solution { + public int mySqrt(int x) { + int left = 0; + int right = x; + int mid = 0; + + if (x == 1 || x == 0) return x; + while (left < right){ + mid = left + (right - left) / 2; + if (x / mid < mid){ + right = mid; + } else if (x / mid > mid) { + left = mid + 1; + } else { + return mid; + } + } + return right - 1; + } +} \ No newline at end of file diff --git a/problems/74_Search_a_2D_Matrix/solution_1.txt b/problems/74_Search_a_2D_Matrix/solution_1.txt new file mode 100644 index 0000000000..fbccec5055 --- /dev/null +++ b/problems/74_Search_a_2D_Matrix/solution_1.txt @@ -0,0 +1,36 @@ +class Solution { + public boolean searchMatrix(int[][] matrix, int target) { + int first = 0; int last = matrix.length - 1; + int left = 0; int right = matrix[0].length - 1; + + while (first < last){ + int mid = first + (last - first) / 2; + if (matrix[mid][0] == target) return true; + else if (matrix[mid][0] > target){ + last = mid - 1; + } else { + first = mid; + } + + if (last - first == 1){ + if (matrix[last][0] <= target){ + first = last; + } else { + break; + } + } + } + + while (left <= right){ + int mid = left + (right - left) / 2; + if (matrix[first][mid] == target) return true; + else if (matrix[first][mid] > target){ + right = mid - 1; + } else { + left = mid + 1; + } + } + + return false; + } +} \ No newline at end of file diff --git a/problems/75_Sort_Colors/solution_1.txt b/problems/75_Sort_Colors/solution_1.txt new file mode 100644 index 0000000000..efe6bfc5c3 --- /dev/null +++ b/problems/75_Sort_Colors/solution_1.txt @@ -0,0 +1,20 @@ +class Solution { + public void sortColors(int[] nums) { + int start = 0; + int last = nums.length - 1; + int i = 0; + + while(i <= last){ + if (nums[i] == 2){ + nums[i] = nums[last]; + nums[last--] = 2; + } else if (nums[i] == 0) { + nums[i] = nums[start]; + nums[start++] = 0; + i = start; + } else { + i++; + } + } + } +} \ No newline at end of file diff --git a/problems/83_Remove_Duplicates_from_Sorted_List/solution_1.txt b/problems/83_Remove_Duplicates_from_Sorted_List/solution_1.txt new file mode 100644 index 0000000000..3d3eada906 --- /dev/null +++ b/problems/83_Remove_Duplicates_from_Sorted_List/solution_1.txt @@ -0,0 +1,32 @@ +/** + * 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 deleteDuplicates(ListNode head) { + ListNode tail = head; + while (tail != null){ + ListNode last = tail; + while (last != null){ + if (last.next == null) { + tail.next = null; + return head; + } + if (last.next.val == last.val){ + last = last.next; + }else { + tail.next = last.next; + tail = tail.next; + break; + } + } + } + return head; + } +} \ No newline at end of file diff --git a/problems/94_Binary_Tree_Inorder_Traversal/solution_1.txt b/problems/94_Binary_Tree_Inorder_Traversal/solution_1.txt new file mode 100644 index 0000000000..67b2641a24 --- /dev/null +++ b/problems/94_Binary_Tree_Inorder_Traversal/solution_1.txt @@ -0,0 +1,27 @@ +/** + * 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 { + ArrayList result = new ArrayList<>(); + public List inorderTraversal(TreeNode root) { + if (root == null) return result; + inorderTraversal(root.left); + result.add(root.val); + inorderTraversal(root.right); + return result; + + } + +} \ No newline at end of file diff --git a/problems/98_Validate_Binary_Search_Tree/solution_1.txt b/problems/98_Validate_Binary_Search_Tree/solution_1.txt new file mode 100644 index 0000000000..1f4080dfe4 --- /dev/null +++ b/problems/98_Validate_Binary_Search_Tree/solution_1.txt @@ -0,0 +1,29 @@ +/** + * 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 boolean isValidBST(TreeNode root) { + return validate(root, null, null); + } + + public boolean validate (TreeNode root, Integer left, Integer right){ + if (root == null) return true; + + if (left != null && root.val <= left) return false; + else if (right != null && root.val >= right) return false; + + return validate(root.left, left, root.val) && validate(root.right, root.val, right); + } +} \ No newline at end of file diff --git a/problems/98_Validate_Binary_Search_Tree/solution_2.txt b/problems/98_Validate_Binary_Search_Tree/solution_2.txt new file mode 100644 index 0000000000..ec8d4460e8 --- /dev/null +++ b/problems/98_Validate_Binary_Search_Tree/solution_2.txt @@ -0,0 +1,39 @@ +/** + * 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 boolean isValidBST(TreeNode root) { + List tra = new ArrayList<>(); + + return validate(root, tra); + } + + private boolean validate(TreeNode node, List tra){ + if (node == null) return true; + boolean result = true; + + boolean left = validate(node.left, tra); + + tra.add(0, node.val); + + if (tra.size() != 1 && tra.get(0) <= tra.get(1)) result = false; + + boolean right = validate(node.right, tra); + + return left && result && right; + + + } +} \ No newline at end of file