aboutsummaryrefslogtreecommitdiff
path: root/search_2d_matrix2.cpp
blob: 9834431c2216acef5ec385f11e87a3ebf3d3fd96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "leetcode.h"

class Solution {
public:
  bool searchMatrix(vector<vector<int>> &matrix, int target) {
    int m = matrix.size(), n = matrix[0].size();
    if (m == 0)
      return false;
    int i = 0, j = n - 1;
    while (i < m && j >= 0) {
      if (matrix[i][j] == target)
        return true;
      else if (matrix[i][j] > target)
        j--;
      else
        i++;
    }
    return false;
  }
};