class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix.length == 0) return false;
int rows = matrix.length;
int cols = matrix[0].length;
int left = 0;
int right = rows*cols-1;
while(left <= right){
int mid = left + (right-left)/2;
int midElement = matrix[mid/cols][mid%cols];
if(midElement == target) return true;
if(midElement < target) left = mid+1;
else right = mid-1;
}
return false;
}
}
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix.length == 0) return false;
int rows = matrix.length;
int cols = matrix[0].length;
int left = 0;
int right = rows*cols-1;
while(left <= right){
int mid = left + (right-left)/2;
int midElement = matrix[mid/cols][mid%cols];
if(midElement == target) return true;
if(midElement < target) left = mid+1;
else right = mid-1;
}
return false;
}
}
Code Example |
---|
:: How to define lambda expression in Java? |
Java :: |
:: |
:: var keyword not working in spring boot application |
:: how to make bidirectional fromated binding |
:: |
:: |
:: |
:: |
:: |
:: |
:: |
:: zweidimensionales array erstellen java |
Java :: |
:: javax.servlet.Filter |
:: |
:: |
:: |
Java :: |
Java :: |
:: |
:: |
:: |
:: |
:: |
Java :: reversing an integer with overflow handled |
:: |
:: |
:: |
:: |