标签:color 二维 一维数组 array max 维数 右上角 ++ scribe
class Solution { public: bool Find(int target, vector<vector<int> > array) { int n = array.size(), m = array[0].size(); for(auto a : array){ for(auto b : a){ if(b == target) return true; } } return false; } };
解法二:
根据题目的意思,每一层行的数都是从左到右递增,并且每一列也都是从上到下递增,因此可以从左下角或者右上角开始找,本篇从左下角开始找,当选择的数 < target时,则向右边继续找,当选择的数 > target时,则向上继续找,一步步缩小搜索范围。
时间复杂度O(m+n)
class Solution { public: bool Find(int target, vector<vector<int> > array) { int maxrow = array.size(); int maxcol = array[0].size(); int i = maxrow - 1; //当前行数 int j = 0; //当前列数 while(j < maxcol && i >= 0){ if(array[i][j] == target) return true; else if(array[i][j] < target) j++; else i--; } return false; } };
标签:color 二维 一维数组 array max 维数 右上角 ++ scribe
原文地址:https://www.cnblogs.com/whisperbb/p/11689439.html