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.

For example,

Consider the following 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]
]

Given target = 5, return true.

Given target = 20, return false.

 1 public class Solution {
 2     public boolean searchMatrix(int[][] matrix, int target) {
 3         if(matrix.length == 0 || matrix[0].length == 0) return false;
 4         int i = 0,j = matrix[0].length-1;
 5         while(i < matrix.length && j >= 0){
 6             if(matrix[i][j] == target) return true;
 7             if(matrix[i][j] < target) i++;
 8             else j--;
 9         }
10         return false;
11     }
12 }

该题矩阵比较特殊,可以从右上角X开始比较, 如果target > X,则到下一行,如果target < X,则要往回走一列。题目在编程之美里面出现过。

解法2: 该题也可以使用二分法进行查找。

时间: 2024-08-28 18:14:42

240. Search a 2D Matrix II java solutions的相关文章

刷题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, in

【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

[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.

[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.

[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

题目: 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.

240 Search a 2D Matrix II 搜索二维矩阵 II

编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性:    每行的元素从左到右升序排列.    每列的元素从上到下升序排列.例如,考虑下面的矩阵:[  [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.给定目标值 target = 20, 返回