diff --git a/LeetCode Solutions/240_Search_a_2D_Matrix_II.cpp b/LeetCode Solutions/240_Search_a_2D_Matrix_II.cpp new file mode 100644 index 00000000..05988bf5 --- /dev/null +++ b/LeetCode Solutions/240_Search_a_2D_Matrix_II.cpp @@ -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> &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>& matrix, int target) { + for(int i=0;itarget) + return 0; + int low=0,high=matrix.size()-1; + while(low> &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]); + } + } + } +}; diff --git a/LeetCode Solutions/Search_a_2D_Matrix_II.cpp b/LeetCode Solutions/Search_a_2D_Matrix_II.cpp deleted file mode 100644 index 2ef54497..00000000 --- a/LeetCode Solutions/Search_a_2D_Matrix_II.cpp +++ /dev/null @@ -1,22 +0,0 @@ -class Solution -{ -public: - bool searchMatrix(vector> &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; - } -}; \ No newline at end of file