From 614c35f59e99ac6d2ce612950e7fbe1b1a030539 Mon Sep 17 00:00:00 2001 From: Shashant Kumar Date: Mon, 3 Jun 2024 13:25:19 +0530 Subject: [PATCH 1/3] solved search-a-2d-matrix-ii --- ...f => 238.Product_of_array_except_self.cpp} | 0 C++/search-a-2d-matrix-ii.cpp | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+) rename C++/{238.Product_of_array_except_self => 238.Product_of_array_except_self.cpp} (100%) create mode 100644 C++/search-a-2d-matrix-ii.cpp diff --git a/C++/238.Product_of_array_except_self b/C++/238.Product_of_array_except_self.cpp similarity index 100% rename from C++/238.Product_of_array_except_self rename to C++/238.Product_of_array_except_self.cpp diff --git a/C++/search-a-2d-matrix-ii.cpp b/C++/search-a-2d-matrix-ii.cpp new file mode 100644 index 00000000..0e760362 --- /dev/null +++ b/C++/search-a-2d-matrix-ii.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + bool searchMatrix(vector>& matrix, int target) { + int n=matrix.size(); + int m=matrix[0].size(); + int r=0,c=m-1; + while(r-1){ + if(matrix[r][c]==target){return true;} + if(matrix[r][c]>target){ + c--; + }else{ + r++; + } + } + return false; + + + } +}; \ No newline at end of file From 00f747c42d9739c5b0c9ce65bebc151a31a3a55c Mon Sep 17 00:00:00 2001 From: Shashant Sah <100856295+shashantsah@users.noreply.github.com> Date: Mon, 3 Jun 2024 13:43:31 +0530 Subject: [PATCH 2/3] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b2cac739..a5487c75 100644 --- a/README.md +++ b/README.md @@ -413,6 +413,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 033 | [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) | [Python](./Python/search-in-rotated-sorted-array.py) | _O(logn)_ | _O(1)_ | Medium | | Binary Search | | 153 | [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) | [Python](./Python/find-minimum-in-rotated-sorted-array.py) | _O(logn)_ | _O(1)_ | Medium | | Binary Search | | 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [C++](./C++/BinarySearch.cpp) | _O(logn)_ | _O(1)_ | Easy | | Binary Search | +| 240 | [Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/) | [C++](./C++/SearchA2DMatrixII.cpp) | _O(m + n)_| _O(1)_ | Medium | | Binary Search |
From 802a9894772974e3fdb12a025179b7e143d540ec Mon Sep 17 00:00:00 2001 From: Shashant Kumar Date: Mon, 3 Jun 2024 14:09:23 +0530 Subject: [PATCH 3/3] added comment also --- C++/search-a-2d-matrix-ii.cpp | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/C++/search-a-2d-matrix-ii.cpp b/C++/search-a-2d-matrix-ii.cpp index 0e760362..c368b53c 100644 --- a/C++/search-a-2d-matrix-ii.cpp +++ b/C++/search-a-2d-matrix-ii.cpp @@ -1,19 +1,31 @@ class Solution { public: bool searchMatrix(vector>& matrix, int target) { - int n=matrix.size(); - int m=matrix[0].size(); - int r=0,c=m-1; - while(r-1){ - if(matrix[r][c]==target){return true;} - if(matrix[r][c]>target){ + // Get the number of rows and columns in the matrix + int n = matrix.size(); + int m = matrix[0].size(); + + // Start from the top-right corner of the matrix + int r = 0; + int c = m - 1; + + // Loop until we reach either the bottom row or the leftmost column + while (r < n and c > -1) { + // If the current element is equal to the target, return true + if (matrix[r][c] == target) { + return true; + } + // If the current element is greater than the target, move left + else if (matrix[r][c] > target) { c--; - }else{ + } + // If the current element is less than the target, move down + else { r++; } } + + // If the target is not found, return false return false; - - } -}; \ No newline at end of file +};