刷题240. Search a 2D Matrix II

一、题目说明

题目240. Search a 2D Matrix II,从一个m*n的二维矩阵查找一个整数,每一行从左到右递增,每一列从上到下递增。

二、我的解答

先计算矩阵中点matrix[row_mid][col_mid],然后将矩阵分成4个区间:

class Solution{
	public:
		bool dfs(vector<vector<int> >& matrix,int target,int start_x, int end_x, int start_y, int end_y){
			if(start_x > end_x || start_y > end_y)
	            return false;
	        if(start_x == end_x && start_y == end_y)
	            return matrix[start_x][start_y] == target;
	        if(target < matrix[start_x][start_y] || target > matrix[end_x][end_y])
	            return false;
	        int mid_x = start_x + (end_x - start_x) / 2;
	        int mid_y = start_y + (end_y - start_y) / 2;
	        if(matrix[mid_x][mid_y] == target) {
	            return true;
	        } else if(matrix[mid_x][mid_y] > target) {
	        	//在1,2,3区域找
	            return dfs(matrix, target, start_x, mid_x - 1, start_y, mid_y - 1)
	                || dfs(matrix, target, mid_x, end_x, start_y, mid_y - 1)
	                || dfs(matrix, target, start_x, mid_x - 1, mid_y, end_y);
	        } else {
	        	//在2,3,4区域找
	            return dfs(matrix, target, mid_x + 1, end_x, mid_y + 1, end_y)
	                || dfs(matrix, target, start_x, mid_x, mid_y + 1, end_y)
	                || dfs(matrix, target, start_x + 1, end_x, start_y, mid_y);
	        }
    	}

		bool searchMatrix(vector<vector<int> >& matrix,int target){
			if(matrix.size()<1){
				return false;
			}
			row = matrix.size();
			col = matrix[0].size();
			bool result = dfs(matrix,target,0,row-1,0,col-1);
			if(result) return true;
			else return false;
		}
	private:
		int row,col;
};

性能如下:

Runtime: 148 ms, faster than 24.09% of C++ online submissions for Search a 2D Matrix II.
Memory Usage: 13.1 MB, less than 6.67% of C++ online submissions for Search a 2D Matrix II.

三、优化措施

从左下角(右上角也行)出发,比较绝妙的解答:

class Solution{
	public:
		//从左下角(或者 右上角)出发,判断
		bool searchMatrix(vector<vector<int> >& matrix,int target){
			if(matrix.size()<1){
				return false;
			}
			row = matrix.size();
			col = matrix[0].size();
			int curRow = row-1;
			int curCol = 0;
			while(curRow<row && curCol<col){
				if(matrix[curRow][curCol] == target) return true;
				else if(matrix[curRow][curCol] < target){
					curCol ++;
				}else if(matrix[curRow][curCol] > target){
					curRow--;
				}
			}
			return false;
		}
	private:
		int row,col;
};

性能如下:

Runtime: 72 ms, faster than 61.20% of C++ online submissions for Search a 2D Matrix II.
Memory Usage: 13 MB, less than 55.56% of C++ online submissions for Search a 2D Matrix II.

原文地址:https://www.cnblogs.com/siweihz/p/12292946.html

时间: 2024-08-09 10:44:18

刷题240. Search a 2D Matrix II的相关文章

【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] 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.

[leedcode 240] 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.

lintcode 中等题:search a 2d matrix II 搜索二维矩阵II

题目 搜索二维矩阵 II 写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每一列的整数从上到下是排序的. 在每一行或每一列中没有重复的整数. 样例 考虑下列矩阵: [     [1, 3, 5, 7],     [2, 4, 7, 8],     [3, 5, 9, 10] ] 给出target = 3,返回 2 挑战 要求O(m+n) 时间复杂度和O(1) 额外空间 解题 直接遍历,时间复杂度是O(MN) public

[LeetCode#240] Search a 2D Matrix II

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 in ascending from left to right. Integers in each column are sorted in ascending from top to

240. Search a 2D Matrix II java solutions

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.

240. 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 bott

Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)

1.问题描述 写一个高效的算法,从一个m×n的整数矩阵中查找出给定的值,矩阵具有如下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 根据矩阵的特征很容易想到二分法,但是这是一个二维的矩阵,如何将问题转化为一维是关键.实际上我们可以根据矩阵的第一列确定值可能所在的行的范围(limu,limd),其中limu=0,使得matrix[0][0]≤matrix[i][0]≤matrix[limd][0],i∈[0,limd].而确定limd的值可以使用二分法.

(medium)LeetCode 240.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.