Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions MergeSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Time Complexity :O(m+n)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :yes
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int p1 = m-1;
int p2 = n-1;
int p3 = m+n-1;
while(p1 >= 0 && p2 >=0){
if(nums2[p2] > nums1[p1]){
nums1[p3] = nums2[p2];
p2--;
}else{
nums1[p3] = nums1[p1];
p1--;
}
p3--;
}
while(p2 >= 0){//copy rest of nums2 to nums1
nums1[p3] = nums2[p2];
p2--;
p3--;
}
}
}

18 changes: 18 additions & 0 deletions RemoveDuplicatesFromSortedArrayII.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Time Complexity :O(n)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :yes
class Solution {
public int removeDuplicates(int[] nums) {
int k=2;
int p1=k;
int p2=k;
while(p2<nums.length){
if(nums[p1-k]!=nums[p2]){
nums[p1]=nums[p2];
p1++;
}
p2++;
}
return p1;
}
}
16 changes: 16 additions & 0 deletions SearchA2DMatrixII.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Time Complexity :O(m+n)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :yes
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length;
int n = matrix[0].length;
int row = m - 1, col = 0;//bottom-left
while(row>=0 && col<n){
if(matrix[row][col] == target) return true;
else if(matrix[row][col] > target) row--;
else col++;
}
return false;
}
}