leetcode-Search a 2D Matrix II-240

输入一个矩阵,其中每一行递增,每一列递增,要求设计一个高效的算法判断target是否在矩阵中

递增,所以二分,lgM*lgN

总体来说就是从右上角往下找到左下角

 1 class Solution {
 2 public:
 3     bool searchMatrix(vector<vector<int> >& matrix, int target) {
 4         int m=matrix.size();
 5         if(m==0) return false;
 6         int n=matrix[0].size();
 7         int b1=n-1;
 8         int ok=1;
 9         int l,r;
10         int a1=0;
11         while(1){
12             if(ok){
13                 ok=0;
14                 l=0,r=b1;
15                 b1=-1;
16                 while(l<=r){
17                     int mid=(l+r)>>1;
18                     int tmp=matrix[a1][mid];
19                     if(tmp==target) return true;
20                     if(tmp<target){
21                         b1=mid;
22                         l=mid+1;
23                     }
24                     else r=mid-1;
25                 }
26                 if(b1==-1) return false;
27             }
28             else{
29                 ok=1;
30                 l=a1,r=m-1;
31                 a1=-1;
32                 while(l<=r){
33                     int mid=(l+r)>>1;
34                     int tmp=matrix[mid][b1];
35                     if(tmp==target) return true;
36                     if(tmp>target){
37                         a1=mid;
38                         r=mid-1;
39                     }
40                     else l=mid+1;
41                 }
42                 if(a1==-1) return false;
43             }
44         }
45         return false;
46     }
47 };
时间: 2024-10-12 23:26:25

leetcode-Search a 2D Matrix II-240的相关文章

leetcode Search a 2D Matrix II

题目连接 https://leetcode.com/problems/search-a-2d-matrix-ii/ Search a 2D Matrix II Description Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascend

[LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom.

LeetCode Search a 2D Matrix II (技巧)

题意: 有一个矩阵,每行有序,每列也有序.判断一个数target是否存在于此矩阵中. 思路: 从右上角开始,如果当前数字<target,则该行作废.若当前数字>target,该列作废.这样下去要么找到,要么到达边界退出. 1 class Solution { 2 public: 3 bool searchMatrix(vector<vector<int>>& matrix, int target) { 4 int n=matrix.size(), m=matri

[LeetCode]Search a 2D Matrix II

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom.

【LeetCode】240. Search a 2D Matrix II

Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascendin

[LeetCode][JavaScript]Search a 2D Matrix II

Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascendin

刷题240. Search a 2D Matrix II

一.题目说明 题目240. Search a 2D Matrix II,从一个m*n的二维矩阵查找一个整数,每一行从左到右递增,每一列从上到下递增. 二.我的解答 先计算矩阵中点matrix[row_mid][col_mid],然后将矩阵分成4个区间: class Solution{ public: bool dfs(vector<vector<int> >& matrix,int target,int start_x, int end_x, int start_y, in

LeetCode -- Search a 2D Matrix &amp; Search a 2D Matrix II

Question: Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the la

leetcode——Search a 2D Matrix 二维有序数组查找(AC)

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro

[leetcode]Search a 2D Matrix @ Python

原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/ 题意: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer o