剑指offer---二维数组中的查找

题目:二维数组中的查找

要求:在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数

1 class Solution {
2 public:
3     bool Find(int target, vector<vector<int> > array) {
4         
5     }
6 };

 

解题代码:

 1 class Solution {
 2 public:
 3     bool Find(int target, vector<vector<int> > array) {
 4         int row = array.size();
 5         int col = array[0].size();
 6         int i = 0;
 7         int j = col-1;
 8         while(i<row && j>=0){
 9             if(target == array[i][j])
10                 return true;
11             else if(target > array[i][j])
12                 i++;
13             else
14                 j--;
15         }  
16         return false;
17     }
18 };

 

posted on 2018-10-23 20:00  wangzhch  阅读(151)  评论(0编辑  收藏  举报

导航