Search a 2D Matrix,在有序矩阵查找,二分查找的变形

问题描述:矩阵每一行有序,每一行的最后一个元素小于下一行的第一个元素,查找。

算法分析:这样的矩阵其实就是一个有序序列,可以使用折半查找算法。

public class SearchInSortedMatrix
{
	public static boolean searchMatrix(int[][] matrix, int target)
	{
		int m = matrix.length;
		int n = matrix[0].length;
		int low = 0;
		int high = m*n - 1;
		while(low <= high)
		{
			int mid = (low + high)/2;
			int row = mid / n;
			int column = mid % n;
			if(matrix[row][column] == target)
			{
				return true;
			}
			else if(matrix[row][column] < target)
			{
				low = mid + 1;
			}
			else
			{
				high = mid - 1;
			}
		}
		return false;
	}
}
时间: 2024-10-12 19:15:21

Search a 2D Matrix,在有序矩阵查找,二分查找的变形的相关文章

leetcode——Search a 2D Matrix 二维有序数组查找(AC)

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro

Leetcode 二分查找 Search a 2D Matrix

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search a 2D Matrix Total Accepted: 10871 Total Submissions: 35629 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in e

leetcode_74题——Search a 2D Matrix(数组查找)

Search a 2D Matrix Total Accepted: 40009 Total Submissions: 127082My Submissions Question Solution Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted fr

[LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom.

[Leetcode] search a 2d matrix 搜索二维矩阵

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro

[LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom.

Search a 2D Matrix II【原创】

Search a 2D Matrix II: python学了还么两周可能有些地方写的很新手大家不要笑我 解题思路: 首先当数组为1维数组时if in 直接查找即可 当为 m x n 维数组时: matrix为行增列增的有序矩阵 所以对行和列采用折半查找,即可判断出值所在的新的矩阵范围,一步步缩小,若存在则某一次的行折半或者列折半肯定会遍历到,否则不存在于矩阵中 每一行的第一个元素为最小值,每一列最后一个元素为最大值 若此行的第一个元素比target要大,那肯定在行号小于此行的行中,若此列的最后

54. Search a 2D Matrix &amp;&amp; Climbing Stairs (Easy)

Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer

LeetCode: Search a 2D Matrix [074]

[题目] Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previo