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..c368b53c --- /dev/null +++ b/C++/search-a-2d-matrix-ii.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + bool searchMatrix(vector>& matrix, int 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--; + } + // If the current element is less than the target, move down + else { + r++; + } + } + + // If the target is not found, return false + return false; + } +}; 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 |