[LeetCode] 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 last integer of the previous row.
For example,
Consider the following matrix:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
Given target = 3, return true.
在一个矩阵中确定是否存在某个数,这个矩阵有规律:一个排序好的数组,一行一行地填入矩阵。解题思路就是先二分查找会在哪一行,然后二分查找找是否存在。
#include<iostream>
#include<vector>
using namespace std; class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
int m=matrix.size();
if(m<) return false;
int n=matrix[].size();
int tm;
//cout<<"m:"<<m<<" n:"<<n<<endl;
if(m==) tm=;
else{
if(matrix[m-][]<=target) tm=m-;
else{
int lft=,rgt=m-;
tm =;
do{
if(matrix[tm+][]>target) break;
int mid = (lft+rgt)/;
if(matrix[mid][]>target) rgt=mid;
else lft=mid;
tm = lft;
}while(lft+<rgt);
}
}
int lft=,rgt=n-;
if(matrix[tm][rgt]==target||matrix[tm][lft]==target) return true;
if(matrix[tm][rgt]<target) return false;
int tn=lft;
do{
int mid=(lft+rgt)/;
if(matrix[tm][mid]>target) rgt = mid;
else lft=mid;
tn = lft;
if(matrix[tm][tn]==target) return true;
}while(lft+<rgt);
return false;
}
}; int main()
{
vector<vector<int> > matrix{{, , , },{, , , },{, , , }};
Solution sol;
cout<<sol.searchMatrix(matrix,)<<endl;
return ;
}
[LeetCode] Search a 2D Matrix 二分搜索的更多相关文章
- [LeetCode] Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [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 follo ...
- LeetCode Search a 2D Matrix II
原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...
- LeetCode: Search a 2D Matrix 解题报告
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- LeetCode -- Search a 2D Matrix & Search a 2D Matrix II
Question: Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matr ...
- LeetCode——Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [leetcode]Search a 2D Matrix @ Python
原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/ 题意: Write an efficient algorithm that sear ...
- [Leetcode] search a 2d matrix 搜索二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- LeetCode Search a 2D Matrix II (技巧)
题意: 有一个矩阵,每行有序,每列也有序.判断一个数target是否存在于此矩阵中. 思路: 从右上角开始,如果当前数字<target,则该行作废.若当前数字>target,该列作废.这样 ...
随机推荐
- 作业hashlib题目
'''1.编写用户认证功能,要求如下 1.1.对用户密码加盐处理 1.2.用户名与密文密码存成字典,是以json格式存到文件中的 1.3.要求密用户输入明文密码,但程序中验证的是密文'''import ...
- JZOJ 3493. 【NOIP2013模拟联考13】三角形
3493. [NOIP2013模拟联考13]三角形(triangle) (File IO): input:triangle.in output:triangle.out Time Limits: 10 ...
- 20181229(守护进程,互斥锁,IPC,生产者和消费者模型)
一.守护进程 守护进程:一个进程B守护另一个进程A,当被守护的进程A结束,进程B也就结束了.(不一定同生,但会同死) 两个特点: ①守护进程会在主进程代码执行结束后就终止 ②守护进程内无法再开启子进程 ...
- C++实例 MySTLString
#include <iostream> #include <cstring> #include <string> using namespace std; clas ...
- 动态规划:最长上升子序列之基础(经典算法 n^2)
解题心得: 1.注意动态转移方程式,d[j]+1>d[i]>?d[i]=d[j]+1:d[i] 2.动态规划的基本思想:将大的问题化为小的,再逐步扩大得到答案,但是小问题的基本性质要和大的 ...
- redis配置密码 redis常用命令
redis配置密码 1.通过配置文件进行配置yum方式安装的redis配置文件通常在/etc/redis.conf中,打开配置文件找到 [plain] view plain copy #requi ...
- 03017_ajax
1.Ajax概述 (1)什么是同步,什么是异步? ①同步现象:客户端发送请求到服务器端,当服务器返回响应之前,客户端都处于等待卡死状态: ②异步现象:客户端发送请求到服务器端,无论服务器是否返回响应, ...
- Spring MVC学习总结
Spring MVC学习总结 Spring MVC学习路(一) 下载配置文件 Spring MVC学习路(二) 设置配置文件 Spring MVC学习路(三) 编写第一个demo Spring MVC ...
- 39、apk瘦身(转载)
本文转自::Android开发中文站 » 关于APK瘦身值得分享的一些经验 从APK的文件结构说起 APK在安装和更新之前都需要经过网络将其下载到手机,如果APK越大消耗的流量就会越多,特别是对于使用 ...
- 内置函数,重要的四个reduce,map,lambda,filter
#filter过滤器#filter(函数,列表)#把列表里的元素序列化,然后在函数中过滤# str=["a","b","c","d ...