lintcode28- Search a 2D Matrix- easy

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.

Example

Consider the following matrix:

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

Given target = 3, return true.

Challange

O(log(n) + log(m)) time



1. 两次binary search

先针对每行第一个元素检索,找到可能行;再在可能行内检索。

public class Solution {
    /*
     * @param matrix: matrix, a list of lists of integers
     * @param target: An integer
     * @return: a boolean, indicate whether matrix contains target
     */
    public boolean searchMatrix(int[][] matrix, int target) {

        //看看这样写行不
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0){
            return false;
        }

        // 对吗
        int rows = matrix.length;
        int cols = matrix[0].length;

        //find the possible row.
        int start = 0;
        int end = rows - 1;
        int possibleRow = 0;
        while (start + 1 < end){
            int mid = start + (end - start) / 2;
            if (target < matrix[mid][0]){
                end = mid;
            } else if (target == matrix[mid][0]){
                return true;
            } else {
                start = mid;
            }
        }
        if (matrix[end][0] > target){
            possibleRow = start;
        } else if (matrix[end][0] == target){
            return true;
        } else {
            possibleRow = end;
        }

        //find in the possible row.
        start = 0;
        cols = matrix[possibleRow].length;
        end = cols - 1;
        while (start + 1 < end){
            int mid = start + (end - start) / 2;
            if (target < matrix[possibleRow][mid]){
                end = mid;
            } else if (target == matrix[possibleRow][mid]){
                return true;
            } else {
                start = mid;
            }
        }
        if (matrix[possibleRow][start] == target || matrix[possibleRow][end] == target){
            return true;
        }
        return false;
    }
}


2.一次binary search

把所有数字连起来看做一个排序一元数组,用/和%提取行列坐标,一次binary search。

public class Solution {
    /*
     * @param matrix: matrix, a list of lists of integers
     * @param target: An integer
     * @return: a boolean, indicate whether matrix contains target
     */
    public boolean searchMatrix(int[][] matrix, int target) {

        if (matrix == null || matrix.length == 0){
            return false;
        }

        if (matrix[0] == null || matrix[0].length == 0){
            return false;
        }

        int rows = matrix.length;
        int cols = matrix[0].length;

        int start = 0;
        int end = rows * cols - 1;

        while (start + 1 < end){
            int mid = start + (end - start) / 2;
            if (target < matrix[mid / cols][mid % cols]){
                end = mid;
            } else if (target == matrix[mid / cols][mid % cols]){
                return true;
            } else {
                start = mid;
            }
        }
        if (matrix[start / cols][start % cols] == target || matrix[end / cols][end % cols] == target){
            return true;
        }
        return false;
    }
}


1.对二元数组的输入判别:

if(matrix == null || matrix.length == 0){
            return false;
}

if(matrix[0] == null || matrix[0].length == 0){
            return false;
}

2.对二元数组的行列长度提取:(要在判别存在行以后才可以用matrix[0])

int row = matrix.length;
int column = matrix[0].length;
时间: 2024-11-15 02:22:31

lintcode28- Search a 2D Matrix- easy的相关文章

Lintcode28 Search a 2D Matrix solution 题解

[题目描述] 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 previo

【leetcode】 Search a 2D Matrix (easy)

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

Search a 2D Matrix (Easy,二分搜索)

题目描述: 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 previ

54. Search a 2D Matrix &amp;&amp; Climbing Stairs (Easy)

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

leetcode Search a 2D Matrix II

题目连接 https://leetcode.com/problems/search-a-2d-matrix-ii/ Search a 2D Matrix II Description 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 ascend

【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——Search a 2D Matrix 二维有序数组查找(AC)

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

[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_74题——Search a 2D Matrix(数组查找)

Search a 2D Matrix Total Accepted: 40009 Total Submissions: 127082My Submissions Question Solution 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 fr

LeetCode: Search a 2D Matrix 解题报告

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