美文网首页Lintcode程序员
Lintcode38 Search a 2D Matrix II

Lintcode38 Search a 2D Matrix II

作者: 代码码着玩 | 来源:发表于2017-04-05 09:39 被阅读31次

    【题目描述】

    Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of it.

    This matrix has the following properties:Integers in each row are sorted from left to right.Integers in each column are sorted from up to bottom.No duplicate integers in each row or column.

    写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数。

    这个矩阵具有以下特性:每行中的整数从左到右是排序的。每一列的整数从上到下是排序的。在每一行或每一列中没有重复的整数。

    【题目链接】

    http://www.lintcode.com/en/problem/search-a-2d-matrix-ii/

    【题目解析】

    O(m + n)解法:

    从矩阵的右上角(屏幕坐标系)开始,执行两重循环

    外循环递增枚举每行,内循环递减枚举列

    O(n ^ 1.58)解法:

    分治法,以矩形中点为基准,将矩阵拆分成左上,左下,右上,右下四个区域

    若中点值 < 目标值,则舍弃左上区域,从其余三个区域再行查找

    若中点值 > 目标值,则舍弃右下区域,从其余三个区域再行查找

    时间复杂度递推式:T(n) = 3T(n/2) + c

    【参考答案】

    http://www.jiuzhang.com/solutions/search-a-2d-matrix-ii/

    相关文章

      网友评论

        本文标题:Lintcode38 Search a 2D Matrix II

        本文链接:https://www.haomeiwen.com/subject/qwjvnttx.html