array: 待查找的二维数组 target:查找的数字
查找到返回true,查找不到返回false
1 class Solution { 2 public: 3 bool Find(vector<vector<int> > array,int target) { 4 bool found = false; 5 if (!array.empty()) 6 { 7 int row = 0; 8 int col = array[0].size() -1; 9 int rows = array.size(); 10 while( row < rows && col >= 0) 11 { 12 if (array[row][col] == target) 13 { 14 found = true; 15 break; 16 } 17 else if (array[row][col] > target) 18 { 19 -- col; 20 } 21 else 22 { 23 ++ row; 24 } 25 } 26 } 27 return found; 28 } 29 };
原文:http://www.cnblogs.com/xiaoyesoso/p/5142358.html