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 each row are sorted from left to right.
  • The first integer of each row is greater than the last integer of the previous row.

For example,

Consider the following matrix:

[
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
]

Given target = 3,
return true.

题意:在一个二维矩阵中找到给定的值。矩阵从上到下从左到右有序

思路:二维空间的二分查找

先在一维里找中间位置,再将该位置转为二维空间里的下标

注:下标比较难弄,得注意点

复杂度: 时间O(log n),空间O(1)

相关题目:

Search Insert Position

bool searchMatrix(const vector<vector<int> > &matrix, int target){
	if (matrix.empty()) return false;
	int m = matrix.size(), n = matrix.front().size();
	int begin = 0, end = m * n, middle, row, col;
	while(begin < end){
		middle = begin + (end - begin) / 2;
		int row = middle / n;
		int col = middle % n;
		if(matrix[row][col] == target) return true;
		else if(matrix[row][col] < target) begin = middle + 1;
		else end = middle;
	}
	return false;
}

Leetcode 二分查找 Search a 2D Matrix,布布扣,bubuko.com

时间: 2024-12-15 01:53:07

Leetcode 二分查找 Search a 2D Matrix的相关文章

Leetcode 二分查找 Search Insert Position

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 Total Submissions: 41575 Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be i

【LeetCode】240. Search a 2D Matrix II

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 ascendin

leetcode 二分查找 Search for a Range

Search for a Range Total Accepted: 21480 Total Submissions: 78454My Submissions Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If

leetcode 二分查找 Search in Rotated Sorted Array

Search in Rotated Sorted Array Total Accepted: 28132 Total Submissions: 98526My Submissions Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to s

leetcode 二分查找 Search in Rotated Sorted ArrayII

Search in Rotated Sorted Array II Total Accepted: 18500 Total Submissions: 59945My Submissions Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a functi

【leetcode】74. Search a 2D Matrix &amp; 240. Search a 2D Matrix II

题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是,如果某一行的最小值都比target要大,那么这一行之后的值都比target要大.如果target介于某一行的最小值和最大值之间,那么target有可能在这一行.至于如何判断target是否存在,因为数组有序,用二分查找即可. 代码如下: class Solution(object): def se

leetcode || 74、Search a 2D Matrix

problem: 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 pr

【一天一道LeetCode】#74. Search a 2D Matrix

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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 lef

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