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

11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a method to find an element.

LeetCode上的原题,请参见我之前的博客Search a 2D Matrix 搜索一个二维矩阵Search a 2D Matrix II 搜索一个二维矩阵之二

class Solution {
public:
bool findElement(vector<vector<int> > &matrix, int elem) {
if (matrix.empty() || matrix[].empty()) return false;
int row = , col = matrix[].size() - ;
while (row < matrix.size() && col >= ) {
if (matrix[row][col] == elem) return true;
else if (matrix[row][col] < elem) ++row;
else --col;
}
return false;
}
};
上一篇:从预编译的角度理解Swift与Objective-C及混编机制


下一篇:.NET微信通过授权获取用户的基本信息