[二分搜索] leetcode 240 Search a 2D Matrix II

mac2022-06-30  40

problem:https://leetcode.com/problems/search-a-2d-matrix-ii

         经典双指针二分查找题目。

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

 

转载于:https://www.cnblogs.com/fish1996/p/11335373.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)