LeetCode - Search a 2D Matrix

5 篇文章 0 订阅
2 篇文章 0 订阅
作者:disappearedgod
时间:2014-6-6

题目


Search a 2D Matrix

  Total Accepted: 12061  Total Submissions: 39463 My Submissions

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]
]

Given target = 3, return true.




解法

剑指Offer第三题解法
public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        if(matrix.length> 0 && matrix[0].length > 0){
            int row = 0;
            int col = matrix.length-1;
            while(col >=0 && row< matrix[0].length){
                if(matrix[col][row] == target)
                    return true;
                else if(matrix[col][row] > target)
                    --col;
                else
                    ++ row;
            }
        }
        return false;
    }
    
}

My Submissions for Search a 2D Matrix
Submit Time Status Run Time Language
0 minutes ago Accepted 512 ms java



一个非常难的想法
这个想法我们没有通过,原因如错误一样:我的想法就是首先列搜索,然后行搜素。列搜索要找到行搜索的方向(用diff衡量target与每行第一列数的差距,然后来搜素这一行)
但是有一个问题就如wrong answer爆出的一样。

public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        
        int xlength = matrix[0].length;
        int ylength=matrix.length;
        if(xlength == 0 && ylength == 0)
            return false;
        int start = -1, end = ylength, ymid, ycmp , diff = Integer.MAX_VALUE, mark = 0;
        int low = -1, high = xlength, xmid, xcmp;
        while((start+1) != end && ylength > 1){
            ymid = start + (end - start)/2;
            ycmp = matrix[ymid][0];
            if(ycmp == target )
                return true;
            else if(ycmp < target){
                start = ymid;
                if(diff > Math.abs(target - ycmp)){
                    diff = Math.abs(target - ycmp);
                    mark = ymid;
                }
                else
                    break;
            }
            else{
                end = ymid;
                if(diff > Math.abs(target - ycmp)){
                    diff = Math.abs(target - ycmp);
                    mark = ymid;
                }
                else
                    break;
            }
            
        }
        while((low+1) != high ){
            xmid = low + (high - low)/2;
            xcmp = matrix[mark][xmid];
            if(xcmp == target){
                return true;
            }
            else if(xcmp < target){
                low = xmid;
            }
            else{
                high = xmid;
            }
        }
        return false;
    }
    
}



Input:[[1,3,5,7],[10,11,16,20],[23,30,34,50]], 7
Output:false
Expected:true

后来想想,可能是判断条件不对,其实我只是想找一个能搜索的阈值,而这个阈值需要x1<target<xn.
public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        
        int xlength = matrix[0].length;
        int ylength=matrix.length;
        if(xlength == 0 && ylength == 0)
            return false;
        int start = -1, end = ylength, ymid, ycmp , mark = 0;
        int low = -1, high = xlength, xmid, xcmp;
        while((start+1) != end && ylength > 1){
            ymid = start + (end - start)/2;
            ycmp = matrix[ymid][0];
            if(ycmp == target )
                return true;
            else if(ycmp < target){
                start = ymid;
                mark = ymid;
                if(matrix[ymid][xlength-1] > target)
                    break;
                
            }
            else{
                end = ymid;
            }
            
        }
        while((low+1) != high ){
            xmid = low + (high - low)/2;
            xcmp = matrix[mark][xmid];
            if(xcmp == target){
                return true;
            }
            else if(xcmp < target){
                low = xmid;
            }
            else{
                high = xmid;
            }
        }
        return false;
    }
    
}


一般解法

由于二维数组存储与一维数组很类似,并且此题设置了行列有序就给我们提供了将其看成一个数组的可能性。
public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        
        int x = matrix[0].length;
        int y = matrix.length;
        int low = -1 ,end = x * y ,mid , cmp , xx, yy ;
        while((low + 1) != end){
            mid = low + (end - low)/2;
            xx = mid % x;
            yy = mid / x;
            cmp = matrix[yy][xx];
            if(cmp == target)
                return true;
            else if(cmp > target){
                end = mid;
            }
            else{
                low = mid;
            }
        }
        return false;
    }
    
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值