leetcode 240. Search a 2D Matrix II

来源:互联网 发布:ios映射软件 编辑:程序博客网 时间:2024/06/14 14:46

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 in ascending from left to right.
  • Integers in each column are sorted in ascending from top to bottom.

For example,

Consider the following matrix:

[  [1,   4,  7, 11, 15],  [2,   5,  8, 12, 19],  [3,   6,  9, 16, 22],  [10, 13, 14, 17, 24],  [18, 21, 23, 26, 30]]

Given target = 5, return true.

Given target = 20, return false.

这道题我真是wrong answer,stackoverflow了无数次啊,血泪史。

我的思路并不好,把矩形分解成一个个正方体,如下图:


比如找15。先找1~25的正方形矩阵,对角线定位到13~19。然后再找绿框和黄框里的数字,如果还是找不到,再递归找26~30的矩阵。

package leetcode;public class Search_a_2D_Matrix_II_240 {public boolean searchMatrix(int[][] matrix, int target) {if(matrix.length==0||matrix[0].length==0){return false;}return search(matrix, 0, matrix.length-1, 0, matrix[0].length-1, target);}public boolean search(int[][] matrix,int xBegin,int xEnd,int yBegin,int yEnd,int target){if(xBegin==xEnd&&yBegin==yEnd){//[[1]] target=1的情况if(matrix[xBegin][yBegin]==target){return true;}else{return false;}}int length=Math.min(xEnd-xBegin, yEnd-yBegin);int i=0;//对于[[1,1]] target=0这种情况//第一个对角线结点就大于target,说明该矩阵中不存在target    if(matrix[xBegin+i][yBegin+i]>target){    return false;    }    for(i=0;i<=length;i++){    if(matrix[xBegin+i][yBegin+i]==target){    return true;    }    else if(matrix[xBegin+i][yBegin+i]>target){    break;    }    }    boolean currentCubeFind=false;    if(i<=length){     for(int j=0;j<i;j++){//找黄框     if(matrix[xBegin+i][yBegin+j]==target){     return true;     }     if(matrix[xBegin+j][yBegin+i]==target){     return true;     }     }    if(i<length){//找绿框    currentCubeFind=currentCubeFind||    search(matrix, xBegin+i+1, xBegin+length, yBegin, yBegin+i, target)||    search(matrix, xBegin, xBegin+i, yBegin+i+1, yBegin+length, target);    }        }   if(currentCubeFind==true){   return true;   }   //接下来找蓝框   if(xEnd-xBegin>yEnd-yBegin){//高比宽长的矩形   return search(matrix, xBegin+length+1, xEnd, yBegin, yEnd, target);   }   else if(xEnd-xBegin<yEnd-yBegin){//宽比高长的矩形   return search(matrix, xBegin, xEnd, yBegin+length+1, yEnd, target);   }   return false;}public static void main(String[] args) {// TODO Auto-generated method stubSearch_a_2D_Matrix_II_240 s=new Search_a_2D_Matrix_II_240();int[][] matrix=new int[][]{     {1,2,3,4,5},     {6,7,8,9,10},     {11,12,13,14,15},     {16,17,18,19,20},     {21,22,23,24,25}};System.out.println(s.searchMatrix(matrix, 15));}}
大神的解法则比我简单明了多了,是O(m+n)的解法。

从右上角开始搜索矩阵,把初始current position设为最右上角的corner. 如果target比当前位置的值大,那么target不可能会在当前位置的行里了(因为当前位置已是本行最大值。 如果target比当前位置的值小,那么target不可能在当前位置的列里了(因为当前位置已是本行最小值)。我们每次排除一行或者一列,所以时间复杂度是 O(m+n) 。

public class Solution {    public boolean searchMatrix(int[][] matrix, int target) {        if(matrix == null || matrix.length < 1 || matrix[0].length <1) {            return false;        }        int col = matrix[0].length-1;        int row = 0;        while(col >= 0 && row <= matrix.length-1) {            if(target == matrix[row][col]) {                return true;            } else if(target < matrix[row][col]) {                col--;            } else if(target > matrix[row][col]) {                row++;            }        }        return false;    }}


原创粉丝点击