[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,该列作废.这样 ...
随机推荐
- java抓取12306火车余票信息
最近在弄一个微信的公众帐号,涉及到火车票查询,之前用的网上找到的一个接口,但只能查到火车时刻表,12306又没有提供专门的查票的接口.今天突然想起自己直接去12306上查询,抓取查询返回的数据包,这样 ...
- 整合mybatis和spring时 Error creating bean with name 'sqlSessionFactory' defined in class path resource
今天在整合mybatis和spring的时候出的错 报错如下 Exception in thread "main" org.springframework.beans.factor ...
- JS进阶篇--JS数组reduce()方法详解及高级技巧
基本概念 reduce() 方法接收一个函数作为累加器(accumulator),数组中的每个值(从左到右)开始缩减,最终为一个值. reduce 为数组中的每一个元素依次执行回调函数,不包括数组中被 ...
- 【Ecshop】后台菜单与权限管理
主要php文件: 1,admin/includes/inc_menu.php ECSHOP管理中心菜单数组--配置菜单组及URL 2,languages/zh_cn/admin/common.php ...
- videojs的使用
[官网]http://www.videojs.com/ videojs就提供了这样一套解决方案,他是一个兼容HTML5的视频播放工具,早期版本兼容所有浏览器,方法是:提供三个后缀名的视频,并在不支持h ...
- win7在某个盘或文件夹中出现右键只能新建文件夹的情况 (2012-12-28-bd 写的日志迁移
至于只能新建文件夹的情况如图: 解决方法是在运行中输入msconfig进入如图: 在系统设置选工具项在选中更改UAC设置点击启动如图: 如图: 直接把通知栏拉到最低确定即可(如果已经是最低了那就随便改 ...
- 使用 Sconfig.cmd 配置服务器核心服务器
使用 Sconfig.cmd 配置服务器核心服务器 适用对象:Windows Server 2012 R2, Windows Server 2012 在 Windows Server 2012 中,你 ...
- OpenStack之虚机冷迁移代码简析
OpenStack之虚机冷迁移代码简析 前不久我们看了openstack的热迁移代码,并进行了简单的分析.真的,很简单的分析.现在天气凉了,为了应时令,再简析下虚机冷迁移的代码. 还是老样子,前端的H ...
- 【4Sum】cpp
题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...
- openpyxl模块介绍
openpyxl模块是一个读写Excel 2010文档的Python库,如果要处理更早格式的Excel文档,需要用到额外的库,openpyxl是一个比较综合的工具,能够同时读取和修改Excel文档.其 ...