diff --git a/MergeSortedArray.java b/MergeSortedArray.java new file mode 100644 index 00000000..63a2a5f5 --- /dev/null +++ b/MergeSortedArray.java @@ -0,0 +1,33 @@ + +// Time Complexity : O(N+M) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : yes +// Three line explanation of solution in plain english + +// Your code here along with comments explaining your approach + +class Solution { + public void merge(int[] nums1, int m, int[] nums2, int n) { + int pointer1 = m-1, pointer2 = n-1, pointer3 = m+n - 1; + while(pointer1 >= 0 && pointer2 >= 0) + { + if(nums2[pointer2] > nums1[pointer1]) + { + nums1[pointer3] = nums2[pointer2]; + pointer2--; + } + else + { + nums1[pointer3] = nums1[pointer1]; + pointer1--; + } + pointer3--; + } + while(pointer2 >= 0) + { + nums1[pointer3] = nums2[pointer2]; + pointer2--; + pointer3--; + } + } +} \ No newline at end of file diff --git a/RemoveDuplicateFromSortedArrayii.java b/RemoveDuplicateFromSortedArrayii.java new file mode 100644 index 00000000..8b4f0a73 --- /dev/null +++ b/RemoveDuplicateFromSortedArrayii.java @@ -0,0 +1,32 @@ + +// Time Complexity : O(N) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : yes +// Three line explanation of solution in plain english + +// Your code here along with comments explaining your approach + +class Solution { + public int removeDuplicates(int[] nums) { + int slow = 0, fast = 0; + int count = 0, k = 2; + while(fast < nums.length) + { + if(0 != fast && nums[fast] == nums[fast - 1]) + { + count++; + } + else + { + count = 1; + } + if(count <= k) + { + nums[slow] = nums[fast]; + slow++; + } + fast++; + } + return slow; + } +} \ No newline at end of file diff --git a/SearchATwoDMatrixII.java b/SearchATwoDMatrixII.java new file mode 100644 index 00000000..b9fbea64 --- /dev/null +++ b/SearchATwoDMatrixII.java @@ -0,0 +1,27 @@ + +// Time Complexity : O(M+N) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : yes +// Three line explanation of solution in plain english + +// Your code here along with comments explaining your approach + +class Solution { + public boolean searchMatrix(int[][] matrix, int target) { + int rows = matrix.length, cols = matrix[0].length; + int row = 0, col = cols - 1; + while(row < rows && col >= 0) + { + if(target == matrix[row][col]) return true; + if(matrix[row][col] < target) + { + row++; + } + else + { + col--; + } + } + return false; + } +}