Middle-题目36:74. Search a 2D Matrix

原创 2016年05月31日 15:58:20

题目原文:
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]
]
题目大意:
在一个满足下列性质的矩阵中寻找一个数:
每一行从左到右,每一列从上到下的数组是递增顺序的。且下一行最小的数比上一行最大的数大。
题目分析:
方法一:既然做过了Middle-题目32,就可以用同样的算法。
方法二:因为在第32题的基础上又加一个限定,所以对每行第一个数二分查找,确定该数可能存在的行,再在行内二分查找。
源码:(language:java)
方法一略。
方法二:

public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int start = 0,end = matrix.length - 1,mid;
        if(start == end) // the matrix has only 1 line
            return binarySeach(matrix[0],target);
        while(start <= end) {
            mid = (start + end) / 2;
            if(matrix[mid][0] == target) 
                return true;
            else if(matrix[mid][0] < target && (mid == matrix.length-1 || matrix[mid+1][0] > target)) // target may be at line of mid
                return binarySeach(matrix[mid],target);
            else if(matrix[mid+1][0] <= target)   //target lies after line of mid
                start = mid + 1;
            else
                end = mid-1;
        }
        return false;
    }
    private  boolean binarySeach(int[] nums, int target) {
        int start = 0, end = nums.length-1, mid;
        while(start <= end) {
            mid = (start + end) / 2;
            if(nums[mid] == target)
                return true;
            else if(nums[mid] < target)
                start = mid + 1;
            else
                end = mid - 1;
        }
        return false;
    }
}

成绩:
方法一:2ms,beats 3.01%,众数1ms,89.18%
方法二:1ms,beats 6.15%

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

LeetCode 74. Search a 2D Matrix

1. 题目描述 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has...

[74] Search a 2D Matrix

对有序二维数组进行二分查找

LeetCode74——Search a 2D Matrix

LeetCode74——Search a 2D Matrix 二维数组查找问题,二重循环暴力搜索肯定是超时的,好在二维数组有一定的规律: 1.每一行从左到右是非递减的 2.每一列从上到下是非递减...

529. Minesweeper\74. Search a 2D Matrix

Minesweeper 题目描述 代码实现529. Minesweeper题目描述Let’s play the minesweeper game (Wikipedia, online game)!Yo...

【leetcode】【74】Search a 2D Matrix

一、问题描述 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has ...

LeetCode 74. Search a 2D Matrix (C++ JavaScript)

LeetCode 74 Search a 2D Matrix C++ && JavaScript 搜索 && 二分查找

leetcode || 74、Search a 2D Matrix

problem: Write an efficient algorithm that searches for a value in an m x n matrix. This matri...

Leetcode 74 Search a 2D Matrix 有序二维数组二叉查找

Leetcode 74 Search a 2D Matrix 在一个二维数组中查找是否存在某元素,该二维数组具有以下特性: - 每一行中的元素都是递增的 - 每一行行中最小的元素比上一行最大的元...

LeetCode Num74_Search a 2D Matrix

问题描述:   Write an efficient algorithm that searches for a value in an m x n matrix. This mat...

[leetcode] 74. Search a 2D Matrix 解题报告

题目链接:https://leetcode.com/problems/search-a-2d-matrix/ Write an efficient algorithm that search...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)