查看: 1864| 回复: 4
跳转到指定楼层
上一主题 下一主题
收起左侧

[二分/排序/搜索] 关于二分法search 2D matrix 的问题

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
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]
]
我在读网上一些博客对于这道题的解法,比如如下:
class Solution {
public:
    /**
     * @param matrix, a list of lists of integers
     * @param target, an integer
     * @return a boolean, indicate whether matrix contains target
     */
    bool searchMatrix(vector<vector<int> > &matrix, int target) {
        // write your code here
        if (matrix.size() == 0) {
            return false;
        }
        if (matrix[0].size() == 0) {
            return false;
        }

        int start = 0;
        int m = matrix.size();
        int n = matrix[0].size();
        int end = m * n - 1;

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

        if (matrix[start / n][start % n] == target) {
            return true;
        }

        if (matrix[end / n][end % n] == target) {
            return true;
        }

        return false;
    }
};

请问以上程序里的这两句是否必要:
  if (matrix[start / n][start % n] == target) {
            return true;
        }

        if (matrix[end / n][end % n] == target) {
            return true;
        }
我试着把这两句注释掉,仍然能跑对。请问那个while循环自己能不能行?为什么非要有这两句的存在呢?

上一篇:java里面comparator的问题
下一篇:二叉树有个指针错了,如何找到这个错误的指针
🔗
stellari 2016-11-29 09:22:01 | 只看该作者
全局:
If the matrix is really small, say m = 2, n = 1; then start and end will be initialized to 0 and 2*1-1=1, respectively. Now start + 1 (=1) is not < end (=1), so the while loop will not be executed at all. When that happens, if you do not add those two "if" lines outside the loop, then the program will always return false regardless of whether target can be found.
回复

使用道具 举报

🔗
John.Tan 2016-11-29 09:22:50 | 只看该作者
全局:
我觉得是需要的,举个例子 加入你的matrix是 [1,2], 1x2的矩阵,start = 0, end = 1,   while()根本不会触发。 你要靠while()后面两个if去判断target有没有。

这题还有另外一个优化。这题可以从左下角开始搜索,每一步只能向上或者向右。如果target比当前点小则向上,如果target比当前点大则向右。 O(m + n)复杂度。
回复

使用道具 举报

🔗
stellari 2016-11-29 10:55:10 | 只看该作者
全局:
John.Tan 发表于 2016-11-29 09:22
我觉得是需要的,举个例子 加入你的matrix是 [1,2], 1x2的矩阵,start = 0, end = 1,   while()根本不会 ...

你说的那个算法是适用于另一道题“每个元素右边或下边的元素都比自身大”的。而楼主这题的条件是“每行第一个元素都比前一行最后一个大”,这个条件比你说的那道题宽松。楼主贴出的算法O(logm + logn)已经是最优解法了。
回复

使用道具 举报

🔗
John.Tan 2016-12-5 03:07:29 | 只看该作者
全局:
stellari 发表于 2016-11-29 10:55
你说的那个算法是适用于另一道题“每个元素右边或下边的元素都比自身大”的。而楼主这题的条件是“每行第 ...

你说对了,他的已经 o(logm + log n)了。。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表