240. Search a 2D Matrix II

来源:互联网 发布:安卓手机变电脑软件 编辑:程序博客网 时间:2024/06/05 08:08

问题描述:

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.

解题思路:

思路是简单地对矩阵进行过滤压缩,首先,通过查找第一行和第一列,确定行与列的范围。然后对于每行进行二分查找。当然先根据有效的最后一列判断是否在对应范围内,如果不在,continue,继续进行查找,直到找到该元素。代码如下:

#include"stdio.h"
typedef int bool ;
#define true 1
#define false 0
bool binarySearch(int *row,int start,int end,int target)
{
    int mid;
    while(start<end){
        mid=(start+end)/2;
        if(row[mid]==target)
        {
            return true;
        }
        else if(row[mid]>target)
        {
            end=mid;
        }
        else {
            start=mid+1;
        }
    }
    return false;
}
bool searchMatrix(int matrix[][5],int matrixRowSize,int matrixColSize,int target){
    int row,col;
    for(col=0;col<matrixColSize;col++){
        if(matrix[0][col]>target)
            break;
        else if(matrix[0][col]==target)
            return true;
    }
    int i,j;
    for(i=1;i<row;i++){
        if(matrix[i][col-1]<target)
            continue;
        else if(matrix[i][col-1]==target)
            return true;
        if(binarySearch(matrix[i],1,col,target))
            return true;
    }
     return false;
}
void main()
{
    int i,j;
   int a[5][5]={{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}};
   if(searchMatrix(a,5,5,9))
    printf("true!");
}

问题描述:

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.

解题思路:

思路是简单地对矩阵进行过滤压缩,首先,通过查找第一行和第一列,确定行与列的范围。然后对于每行进行二分查找。当然先根据有效的最后一列判断是否在对应范围内,如果不在,continue,继续进行查找,直到找到该元素。代码如下:

#include"stdio.h"
typedef int bool ;
#define true 1
#define false 0
bool binarySearch(int *row,int start,int end,int target)
{
    int mid;
    while(start<end){
        mid=(start+end)/2;
        if(row[mid]==target)
        {
            return true;
        }
        else if(row[mid]>target)
        {
            end=mid;
        }
        else {
            start=mid+1;
        }
    }
    return false;
}
bool searchMatrix(int matrix[][5],int matrixRowSize,int matrixColSize,int target){
    int row,col;
    for(col=0;col<matrixColSize;col++){
        if(matrix[0][col]>target)
            break;
        else if(matrix[0][col]==target)
            return true;
    }
    int i,j;
    for(i=1;i<row;i++){
        if(matrix[i][col-1]<target)
            continue;
        else if(matrix[i][col-1]==target)
            return true;
        if(binarySearch(matrix[i],1,col,target))
            return true;
    }
     return false;
}
void main()
{
    int i,j;
   int a[5][5]={{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}};
   if(searchMatrix(a,5,5,9))
    printf("true!");
}