剑指offer 二维数组查找

#include <iostream>
#include <vector>

using namespace std;


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


int main() {
    Solution s;
    vector<vector<int>> nums = {
            {0, 3, 5},
            {1, 4, 8},
            {2, 6, 10}
    };
    cout << s.Find(4, nums) << endl;
    cout << s.Find(10, nums) << endl;
    cout << s.Find(1, nums) << endl;
    cout << s.Find(7, nums) << endl;
    cout << s.Find(11, nums) << endl;
    cout << s.Find(1113, nums) << endl;
    return 0;
}
posted @ 2018-08-21 17:20  一条图图犬  阅读(75)  评论(0编辑  收藏  举报