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
33 changes: 33 additions & 0 deletions MergeSortedArray.java
Original file line number Diff line number Diff line change
@@ -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--;
}
}
}
32 changes: 32 additions & 0 deletions RemoveDuplicateFromSortedArrayii.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
27 changes: 27 additions & 0 deletions SearchATwoDMatrixII.java
Original file line number Diff line number Diff line change
@@ -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;
}
}