2024/9/9 Hot100 matrix 最后一题
This commit is contained in:
parent
92cb0cae5a
commit
4d20f5dee9
|
@ -0,0 +1,33 @@
|
||||||
|
package com.cool.hot100.matrix;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
*
|
||||||
|
* @Author: Cool
|
||||||
|
* @Date: 2024/09/09/09:08
|
||||||
|
* DayNumber 1
|
||||||
|
* Hard 2
|
||||||
|
* Level ?
|
||||||
|
*/
|
||||||
|
public class Num240 {
|
||||||
|
|
||||||
|
public boolean searchMatrix(int[][] matrix, int target) {
|
||||||
|
if (matrix.length == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
int colLen = matrix[0].length;
|
||||||
|
int len = matrix.length;
|
||||||
|
int col = colLen - 1;
|
||||||
|
int row = 0;
|
||||||
|
while (col >= 0 && row < len) {
|
||||||
|
if (matrix[row][col] == target) {
|
||||||
|
return true;
|
||||||
|
} else if (matrix[row][col] < target) {
|
||||||
|
row++;
|
||||||
|
} else {
|
||||||
|
col--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue