leetcode75:search-a-2d-matrix
题目描述
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
要搜索的目标值为3,返回true;
- 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, returntrue.
输出
false
class Solution {
public:
/**
*
* @param matrix int整型vector<vector<>>
* @param target int整型
* @return bool布尔型
*/
bool searchMatrix(vector<vector<int> >& matrix, int target) {
// write code here
int rows=matrix.size();
int cols=matrix[0].size();
int begin=0,end=rows*cols-1;
while (begin <= end)
{
int mid=(begin+end)/2;
int row=mid/cols;
int col=mid%cols;
if (matrix[row][col] ==target)
return true;
else if (matrix[row][col]<target)
begin=mid+1;
else
end=mid-1;
}
return false;
}
};
leetcode75:search-a-2d-matrix的更多相关文章
- [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 搜索一个二维矩阵
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
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- 54. Search a 2D Matrix && Climbing Stairs (Easy)
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵
11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...
- 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, ret ...
- LeetCode Search a 2D Matrix II
原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...
- LintCode 38. Search a 2D Matrix II
Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of ...
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
随机推荐
- Appium自动化测试之环境安装
安装前准备: Python 安装包下载 选择想要想在的python包Node-v6.11.2下载安卓SDK下载appium_forwindows下载 以上四个文件下载下来后,分别解压安装, ...
- [HAOI 2017]八纵八横
线段树分治+线形基. 线段树分治是个锤子?? 以时间轴构建线段树,把每个环以"对线段树产生影响的时间区间"的形式加入线段树即可. #include<bits/stdc++.h ...
- RHSA-2018:0007-重要: 内核 安全更新(需要重启、存在EXP)
[root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) 修复命令: 使用root账号登陆She ...
- Go语言中的常见的几个坑
目录 1.for range 2.defer与闭包 3.map内存溢出 4.协程泄漏 5.http手动关闭 记录一下日常中遇到的几个坑,加深一下印象. 1.for range 这个是比较常见的问题了, ...
- es使用--新建、删除、增删改数据
# 进入bin目录 cd /czz/elsearch/bin # 后台启动(不加-d参数则是前台启动,日志在控制台) # 后台启动日志如果不配置,在es目录的logs下面 ./elasticsearc ...
- Python爬虫框架--Scrapy安装以及简单实用
scrapy框架 框架 -具有很多功能且具有很强通用性的一个项目模板 环境安装: Linux: pip3 install scrapy Windows: ...
- boost之multiprecision
multiprecision boost中提供的高精度库,支持高精度整型,浮点型等.并且提供统一的接口模板,只需要指定对应的后端类型即可实现对应类型的高精度计算: boost::multiprecis ...
- 基于python实现链式栈
""" 链式栈 linkstack.py 思路分析: 1.源于链表结构 2.封装栈的操作方法(入栈,出栈,栈空,栈顶) 3.链表的开头作为栈顶(不用每次遍历,效率高,怎样 ...
- go读取excel表格数据
go读取excel表格数据 使用工具 github.com/Luxurioust/excelize 百度到的都是使用这个 实际上已经改名了 github.com/360EntSecGroup-Skyl ...
- 转 Swoole】用swoole简单实现MySQL连接池
MySQL连接池 在传统的网站开发中,比如LNMP模式,由Nginx的master进程接收请求然后分给多个worker进程,每个worker进程再链接php-fpm的master进程,php-fpm再 ...