LeetCode 74: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 row.

For example,

Consider the following matrix:

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

Given target = 3, return true.

Subscribe to see which companies asked this question

方法一:

1.先在所有行中利用二分查找target所在行;

2.然后target所在行进行二分查找target

 class Solution {
 public:
	 bool searchMatrix(vector<vector<int>>& matrix, int target) {
		 if (matrix.size() == 0)     return false;
		 if (matrix[0][0] > target)  return false;
		 int r = matrix.size(), c = matrix[0].size(); //r,c分别为矩阵matrix的行数和列数
		 int low = 0, high = r-1;

		 //在所有行中利用二分查找target所在的行
		while(low <= high)
		 {
			 int mid = low + (high-low) / 2;
			 if (matrix[mid][0] == target)
				 return true;
			 else if (matrix[mid][0] < target)
				 low = mid + 1;
			 else
				 high = mid - 1;
		 }

		//在target所在行中利用二分查找target
		int low1 = 0, high1 = c - 1;
		int k = 0;
		while (low1 <= high1)
		{
			int mid1 = low1 + (high1 - low1)/2;
			if (matrix[low-1][mid1] == target) //注意target所在的行是(low-1)
				return true;
			else if (matrix[low-1][mid1] < target)
				low1 = mid1 + 1;
			else
				high1 = mid1 - 1;

			k = mid1;
		}
		
		if (matrix[low-1][k] == target) 
			return true;
		else
			return false;
	 }
 };


方法二:将矩阵看成数组,利用二分查找,运行时间12ms,时间复杂度O(log(mn)),将m*n的矩阵看成数组,即matrix[x][y]=>a[x*n+y],数组也可以转回m*n矩阵,即a[x]=>matrix[x/n][x%n]

注:此方法为参考leetcode solution中的C++ 12ms方法

//方法二:
 //将矩阵看成数组,利用二分查找,运行时间12ms,时间复杂度O(log(mn))
 //将m*n的矩阵看成数组,即matrix[x][y]=>a[x*n+y]
 //数组也可以转回m*n矩阵,即a[x]=>matrix[x/n][x%n]
 class Solution {
 public:
	 bool searchMatrix(vector<vector<int>>& matrix, int target) {
		 if (matrix.empty())  return false;
		 int m = matrix.size(), n = matrix[0].size();
		 int low = 0, high = m*n - 1;

		 while (low<=high)
		 {
			 int mid = low + (high - low) / 2;
			 int value = matrix[mid / n][mid%n];
			 if (value == target)
				 return true;
			 else if (value < target)
				 low = mid + 1;
			 else
				 high = mid - 1;
		 }
		 return false;
	 }
 };


测试代码:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

 class Solution {
 public:
	 bool searchMatrix(vector<vector<int>>& matrix, int target) {
		 if (matrix.size() == 0)     return false;
		 if (matrix[0][0] > target)  return false;
		 int r = matrix.size(), c = matrix[0].size(); //r,c分别为矩阵matrix的行数和列数
		 int low = 0, high = r-1;

		 //在所有行中利用二分查找target所在的行
		while(low <= high)
		 {
			 int mid = low + (high-low) / 2;
			 if (matrix[mid][0] == target)
				 return true;
			 else if (matrix[mid][0] < target)
				 low = mid + 1;
			 else
				 high = mid - 1;
		 }

		//在target所在行中利用二分查找target
		int low1 = 0, high1 = c - 1;
		int k = 0;
		while (low1 <= high1)
		{
			int mid1 = low1 + (high1 - low1)/2;
			if (matrix[low-1][mid1] == target) //注意target所在的行是(low-1)
				return true;
			else if (matrix[low-1][mid1] < target)
				low1 = mid1 + 1;
			else
				high1 = mid1 - 1;

			k = mid1;
		}
		
		if (matrix[low-1][k] == target) 
			return true;
		else
			return false;
	 }
 };


int main()
{
	Solution s;
	vector<vector<int>> nums1 = { { 1, 3, 5, 7 }, { 10, 11, 16, 20 }, {23,30,34,50} };
	bool  t = s.searchMatrix(nums1, 11);
	cout << t <<endl;
	system("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值