search-a-2d-matrix(二维矩阵查找)

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, returntrue.

class Solution {
public:
    bool searchMatrix(vector<vector<int> > &matrix, int target) {
        int m = matrix.size();
        int n = matrix[0].size();
        int c = n-1;
        for(int r = 0;(r<m)&&(c>=0);)
        {
            if(matrix[r][c]>target)
            {
                c--;
            }
            else if(matrix[r][c]<target)
            {
                r++;
            }
            else
                return true;
        }
        return false;
    }
    //每次比较行末,由于已经排序好,可以进行行列删除
};

  

时间: 2024-10-11 21:26:05

search-a-2d-matrix(二维矩阵查找)的相关文章

leetcode——Search a 2D Matrix 二维有序数组查找(AC)

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 ro

Search a 2D Matrix,在有序矩阵查找,二分查找的变形

问题描述:矩阵每一行有序,每一行的最后一个元素小于下一行的第一个元素,查找. 算法分析:这样的矩阵其实就是一个有序序列,可以使用折半查找算法. public class SearchInSortedMatrix { public static boolean searchMatrix(int[][] matrix, int target) { int m = matrix.length; int n = matrix[0].length; int low = 0; int high = m*n

20140920百度笔试题一道之二维矩阵查找

题目: 有这样一个二维矩阵A[N][N],满足j < k时, 1)a[i][j] < a[i][k]; 2)a[j][i] < a[k][i](其实就数据从左上角到右下角纵横方向上都递减),给定一个数target,如何快速搜索是否在这个矩阵中,是的话输出二维坐标,否则输出Null:(不妨假设数据不重复) 比如  12  34  56  78  90  96 13  35  57  79  91  97 14  36  58  80  93  98 15  37  59  81  94  

关于有序二维矩阵查找和字符串替换的两道算法题

最近看一本书上写到的两个面试题 于是实现了一下 感觉思路很好,大牛略过 : 1.对于一个二维矩阵,从左到右  从上到下 都是递增的,如何判断一个值是否在矩阵内部?(C实现  实现复杂度 O(n)) bool FindInTwoDimensionalMatrix(int*pMatrix,int iRows,int iCols,int iFindVal) { bool bFind=false ; if(pMatrix==0||iRows<=0||iCols<=0) return bFind ; i

刷题240. Search a 2D Matrix II

一.题目说明 题目240. Search a 2D Matrix II,从一个m*n的二维矩阵查找一个整数,每一行从左到右递增,每一列从上到下递增. 二.我的解答 先计算矩阵中点matrix[row_mid][col_mid],然后将矩阵分成4个区间: class Solution{ public: bool dfs(vector<vector<int> >& matrix,int target,int start_x, int end_x, int start_y, in

[LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二

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.

[Leetcode] search a 2d matrix 搜索二维矩阵

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 ro

[LeetCode] Search a 2D Matrix 搜索一个二维矩阵

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 ro

[LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II

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.