LeetCode - Search a 2D Matrix

来源:互联网 发布:天猫淘宝商城包包 编辑:程序博客网 时间:2024/06/05 01:18
作者:disappearedgod
文章出处:http://blog.csdn.net/disappearedgod/article/details/28867615
时间:2014-6-6

题目


Search a 2D Matrix

 Total Accepted: 12061 Total Submissions: 39463My 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 TimeStatusRun TimeLanguage0 minutes agoAccepted512 msjava



一个非常难的想法
这个想法我们没有通过,原因如错误一样:我的想法就是首先列搜索,然后行搜素。列搜索要找到行搜索的方向(用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]], 7Output:falseExpected: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;    }    }







0 0
原创粉丝点击