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
22 changes: 22 additions & 0 deletions RemoveDuplicatesFromSortedArray2
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int count = 1;
int ptr2 = 1;


for (int ptr1 = 1; ptr1 < nums.size(); ptr1++) {
if (nums[ptr1] == nums[ptr1-1]) {
count++;
} else {
count=1;
}

if (count <= 2) {
nums[ptr2] = nums[ptr1];
ptr2++;
}
}
return ptr2;
}
};
29 changes: 29 additions & 0 deletions mergeSortedArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//mergeSortedArray

class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int ptr1=m-1;
int ptr2=n-1;
int endptr = m+n-1;

while (ptr1 >= 0 && ptr2 >= 0) {
if (nums1[ptr1] <= nums2[ptr2]) {
nums1[endptr] = nums2[ptr2];
ptr2--;
} else {
nums1[endptr] = nums1[ptr1];
ptr1--;
}
endptr--;
}

while (ptr2 >= 0 ) {
nums1[endptr] = nums2[ptr2];
ptr2--;
endptr--;
}


}
};
29 changes: 29 additions & 0 deletions searchIn2DMatrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int row=matrix.size();
if (row==0) {
return false;
}
int col=matrix[0].size();
if (col ==0) {
return false;
}

int left = 0;
int right = col-1;
while (left < row && right >= 0) {
if (matrix[left][right] == target) {
return true;
}

if (matrix[left][right] < target) {
left++;
} else {
right--;
}
}

return false;
}
};