74. Search a 2D Matrix 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 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.

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

该题矩阵也是有特点的。 对比题目:

http://www.cnblogs.com/guoguolan/p/5620209.html

解法二: 可以使用二分查找做,不过应该没有利用矩阵的特点来做速度快。

时间: 2024-10-19 08:49:19

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

leetcode 74 Search a 2D Matrix ----- java

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

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 ro

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

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 ro

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 ro

LeetCode 74 Search a 2D Matrix(搜索2D矩阵)

翻译 写一个高效算法用于在一个m x n的矩阵中查找一个值. 这个矩阵有如下属性: 每行的整型数都是从左到右排序的. 每行的第一个元素都比上一行的最后一列大. 例如, 考虑如下矩阵: [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] 给定target = 3,返回true. 原文 Write an efficient algorithm that searches for a value in an m x n matrix. This m

74. Search a 2D Matrix (Graph; Divide-and-Conquer)

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 prev

[leedcode 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 ro

【一天一道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