题目描述
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
C++11(clang++ 3.9)
class Solution { public: bool Find(int target, vector<vector<int> > array) { // 1 3 4 5 // 2 5 8 9 // 5 6 11 13 // 8 9 12 15 // test case: target = 11 int row_count = array.size(); if(row_count <= 0) return false; int col_count = array[0].size(); if(col_count <= 0) return false; int row = 0; int col = col_count - 1; while(row < row_count && col >= 0) { if(array[row][col] == target) return true; else if(array[row][col] < target) row++; else col--; } return false; } };
原文链接: https://www.cnblogs.com/hotwater99/p/12432539.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/333937
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!