标签:containe line boolean lin lse head math tps lan
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] == target) {
return true;
}
}
}
return false;
}
}
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix.length == 0 || matrix[0].length == 0) {
return false;
}
int flag = matrix.length - 1;
for (int i = matrix.length - 1; i >= 0; i--) {
if (matrix[i][0] <= target) {
flag = i;
break;
}
}
int left = 0;
int right = matrix[0].length - 1;
while (left <= right) {
int mid = left + ((right - left) / 2);
if (target < matrix[flag][mid]) {
right = mid - 1;
} else if (target > matrix[flag][mid]) {
left = mid + 1;
} else {
return true;
}
}
return false;
}
}
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix.length == 0) {
return false;
}
int x = matrix.length;
int y = matrix[0].length;
int left = 0;
int right = x * y - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
// 求行
int row = mid / y;
// 求列
int col = mid % y;
if (matrix[row][col] < target) {
left = mid + 1;
} else if (matrix[row][col] > target) {
right = mid - 1;
} else {
return true;
}
}
return false;
}
}
标签:containe line boolean lin lse head math tps lan
原文地址:https://www.cnblogs.com/linzedian/p/14054850.html