搜索二维矩阵1
编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性:
每行中的整数从左到右按升序排列。
每行的第一个整数大于前一行的最后一个整数。
示例 1:
输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
输出:true
示例 2:
输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
输出:false
两次二分查找
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int rows = matrix.length, cols = matrix[0].length;
int left = 0, right = rows - 1;
int r = 0;
while (left <= right) {
int mid = left + (right - left) / 2;
if (matrix[mid][0] == target) {
return true;
}else if (matrix[mid][0] < target) {
left = mid + 1;
r = mid;
} else {
right = mid - 1;
}
}
left = 0;
right = cols - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (matrix[r][mid] == target) {
return true;
}else if (matrix[r][mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return false;
}
}
一次二分查找
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length, n = matrix[0].length;
int left = 0, right = m * n - 1;
int pivotIdx, pivotElement;
while (left <= right) {
int index = (left + right) / 2;
int num = matrix[index / n][index % n];
if (target == num) {
return true;
} else if (target < num) {
right = index - 1;
} else {
left = index + 1;
}
}
return false;
}
}
搜索二维矩阵2
编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target 。该矩阵具有以下特性:
- 每行的元素从左到右升序排列。
- 每列的元素从上到下升序排列。
示例 1:
输入:matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
输出:true
示例 2:
输入:matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
输出:false
二分查找
class Solution {
private boolean binarySearch(int[][] matrix, int target, int start, boolean vertical) {
int low = start;
int high = vertical ? matrix[0].length - 1 : matrix.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (vertical) {
if (matrix[start][mid] < target)
low = mid + 1;
else if (matrix[start][mid] > target)
high = mid - 1;
else
return true;
} else {
if (matrix[mid][start] < target)
low = mid + 1;
else if (matrix[mid][start] > target)
high = mid - 1;
else
return true;
}
}
return false;
}
public boolean searchMatrix(int[][] matrix, int target) {
int shorterDim = Math.min(matrix.length, matrix[0].length);
for (int i = 0; i < shorterDim; i++) {
if (binarySearch(matrix, target, i, true))
return true;
if (binarySearch(matrix, target, i, false))
return true;
}
return false;
}
}
搜索空间缩减
将矩阵划分为四块,左上和右下不包含目标。
class Solution {
private int[][] matrix;
private int target;
private boolean searchRec(int left, int up, int right, int down) {
if (left > right || up > down || target < matrix[up][left] || target > matrix[down][right])
return false;
int mid = left + (right - left) / 2;
int row = up;
while (row <= down && matrix[row][mid] <= target) {
if (matrix[row][mid] == target)
return true;
row++;
}
return searchRec(left, row, mid-1, down) || searchRec(mid+1, up, right, row-1);
}
public boolean searchMatrix(int[][] matrix, int target) {
this.matrix = matrix;
this.target = target;
return searchRec(0, 0, matrix[0].length-1, matrix.length-1);
}
}
从左下角开始查找
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int row = matrix.length - 1, col = 0;
while (row >= 0 && col < matrix[0].length) {
if (matrix[row][col] > target)
row--;
else if (matrix[row][col] < target)
col++;
else
return true;
}
return false;
}
}
猜数字大小
猜数字游戏的规则如下:
- 每轮游戏,我都会从 1 到 n 随机选择一个数字。 请你猜选出的是哪个数字。
- 如果你猜错了,我会告诉你,你猜测的数字比我选出的数字是大了还是小了。
你可以通过调用一个预先定义好的接口 int guess(int num) 来获取猜测结果,返回值一共有 3 种可能的情况(-1,1 或 0):
- -1:我选出的数字比你猜的数字小 pick < num
- 1:我选出的数字比你猜的数字大 pick > num
- 0:我选出的数字和你猜的数字一样。恭喜!你猜对了!pick == num
返回我选出的数字。
示例 1:
输入:n = 10, pick = 6
输出:6
示例 2:
输入:n = 1, pick = 1
输出:1
示例 3:
输入:n = 2, pick = 1
输出:1
示例 4:
输入:n = 2, pick = 2
输出:2
二分查找
public class Solution extends GuessGame {
public int guessNumber(int n) {
int left = 1, right = n;
while (left <= right) {
int mid = left + (right - left) / 2;
int ret = guess(mid);
if (ret == 0)
return mid;
else if (ret > 0)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
}