Skip to content

Commit

Permalink
Merge pull request #633 from GaurishIIITNR/PR3
Browse files Browse the repository at this point in the history
Added Leetcode Problem 48_Rotate_Image
  • Loading branch information
gantavyamalviya authored Oct 5, 2022
2 parents 6a8c5ab + e6328b8 commit 12a26d0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 22 deletions.
51 changes: 51 additions & 0 deletions LeetCode Solutions/240_Search_a_2D_Matrix_II.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Name : Search_a_2D_Matrix_II
// Leetcode Problem 240 (Medium)
// Url : https://leetcode.com/problems/search-a-2d-matrix-ii/

// Approach 1 (Best Approach)
class Solution
{
public:
bool searchMatrix(vector<vector<int>> &matrix, int target)
{
int j = matrix[0].size() - 1, i = 0;
while (1)
{
if (j < 0 || i > matrix.size() - 1)
return 0;
if (matrix[i][j] > target)
j--;
else if (matrix[i][j] < target)
i++;
else
return 1;
}
if (matrix[i][j] == target)
return 1;
return 0;
}
};

// Approach 2

class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
for(int i=0;i<matrix[0].size();i++)
{
if(matrix[0][i]>target)
return 0;
int low=0,high=matrix.size()-1;
while(low<high){
int mid=low+(high-low+1)/2;
if(matrix[mid][i]<=target)
low=mid;
else
high=mid-1;
}
if(matrix[low][i]==target)
return 1;
}
return 0;
}
};
26 changes: 26 additions & 0 deletions LeetCode Solutions/48_Rotate_Image.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Name: Rotate Image(Medium)
// Url : https://leetcode.com/problems/rotate-image
class Solution
{
public:
void rotate(vector<vector<int>> &mt)
{
int n = mt.size();
int tmp = 0;
for (int i = 0; i < n; i++)
{
for (int j = tmp; j < n; j++)
{
swap(mt[i][j], mt[j][i]);
}
tmp++;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < (n / 2); j++)
{
swap(mt[i][j], mt[i][n - j - 1]);
}
}
}
};
22 changes: 0 additions & 22 deletions LeetCode Solutions/Search_a_2D_Matrix_II.cpp

This file was deleted.

0 comments on commit 12a26d0

Please sign in to comment.