g0201_0300.s0240_search_a_2d_matrix_ii.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java17 Show documentation
Show all versions of leetcode-in-java17 Show documentation
Java Solution for LeetCode algorithm problems, continually updating
package g0201_0300.s0240_search_a_2d_matrix_ii;
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Matrix
// #Divide_and_Conquer
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int r = 0;
int c = matrix[0].length - 1;
while (r < matrix.length && c >= 0) {
if (matrix[r][c] == target) {
return true;
} else if (matrix[r][c] > target) {
c--;
} else {
r++;
}
}
return false;
}
}