From 3f2932740d5a9b1edd41707f44e34f670b74af4d Mon Sep 17 00:00:00 2001 From: ankit351104 <91881708+ankit351104@users.noreply.github.com> Date: Thu, 27 Oct 2022 17:08:24 +0530 Subject: [PATCH] Search a 2D matrix (Leetcode 240) I've commented out each line so that whoever is reading can get a better understanding of the Code. --- Medium/Search a 2D Matrix II.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Medium/Search a 2D Matrix II.java diff --git a/Medium/Search a 2D Matrix II.java b/Medium/Search a 2D Matrix II.java new file mode 100644 index 0000000..ff26178 --- /dev/null +++ b/Medium/Search a 2D Matrix II.java @@ -0,0 +1,22 @@ +class Solution { + public boolean searchMatrix(int[][] matrix, int target) { + int row = 0;//Initially we are at integer 1 as per example 1 + int col = matrix[0].length-1;//at element 15 as per example 1 + while(row=0){ + if(target==matrix[row][col]){ + return true; + } + if(targettarget so decrease column by 1 and check, here if we decrease by 1 we will be on 11 + col--; + continue; + } + if(target>matrix[row][col]){ + //moving forward we will end up at 4 which is lesser than target and + //as the matrix is sorted we won't get the element in that row so we will switch the row + row++; + } + } + return false; //if the target element is not present + } +} \ No newline at end of file