博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
lintcode28- Search a 2D Matrix- easy
阅读量:5026 次
发布时间:2019-06-12

本文共 3519 字,大约阅读时间需要 11 分钟。

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;

 

转载于:https://www.cnblogs.com/jasminemzy/p/7572702.html

你可能感兴趣的文章
uva6152Bits Equalizer
查看>>
linux install Openvino
查看>>
struts2中action如何获取jsp页面参数
查看>>
华为机试题
查看>>
Leetcode Delete Node in a Linked List
查看>>
mybatis中二级缓存整合ehcache实现分布式缓存
查看>>
Lucene中的Ram存储
查看>>
document.compatMode
查看>>
do_try_to_free_pages
查看>>
attribute和property的区别
查看>>
python爬取百度搜索图片
查看>>
排序之希尔排序(shell sort)
查看>>
Linux学习之CentOS(二十一)--Linux系统启动详解
查看>>
Hadoop的体系结构之MapReduce的体系结构
查看>>
python学习(四)字符串学习
查看>>
互联网协议入门(一)
查看>>
Python自动化开发从浅入深-语言基础(常用模块)
查看>>
eclipse中properties文件编码问题
查看>>
JavaIO学习
查看>>
PHP+MYSQL网站SQL Injection攻防
查看>>