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, returntrue.
有序二维矩阵找数,先确定范围再二分查找
class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
int m=matrix.size(),n=matrix[].size();
int i=;
for(i=;i<m;i++){
if(target>=matrix[i][]&&target<=matrix[i][n-])
break;
}
if(i==m) return false;
int left=,right=n-;
while(left<=right){
int mid=(left+right)/;
if(target==matrix[i][left]||target==matrix[i][right]||target==matrix[i][mid])
return true;
if(target<matrix[i][mid])
right=mid-;
else
left=mid+;
}
return false;
}
};
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 ...
- 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 follo ...
- Search a 2D Matrix,在有序矩阵查找,二分查找的变形; 行有序,列有序查找。
问题描述:矩阵每一行有序,每一行的最后一个元素小于下一行的第一个元素,查找. 算法分析:这样的矩阵其实就是一个有序序列,可以使用折半查找算法. public class SearchInSortedM ...
- [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 ...
- [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 ...
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- [LeetCode] 74. 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] 240. Search a 2D Matrix II 搜索一个二维矩阵 II
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- GPS经纬度的表示方法及换算
想要认识GPS中的经纬度,就必须先了解GPS,知道经纬度的来源: 1. GPS系统组成 GPS是 Gloabal Positioning System 的简称,意为全球定位系统,主要由地面的控制站.天 ...
- 生活大爆炸版锤子剪刀布(NOIP2014)(真·模拟)
水!水!!水!!!(重要的事情说三遍..) 1分钟代码题.. 原题传送门 这题啊,手打BOOL判断,然后乱搞啊.. 这有什么难的.. 加个快读就能拿第一啦.. 膜9018上各位大佬们.. 下面贴代码 ...
- linux 内存查看方法:meminfo\maps\smaps\status 文件解析
linux 下面查看内存有多种渠道,比如通过命令 ps ,top,free 等,比如通过/proc系统,一般需要比较详细和精确地知道整机内存/某个进程内存的使用情况,最好通过/proc 系统,下面介绍 ...
- 使用timeit模块 测试两种方式生成列表的所用的时间
from timeit import Timer def test(): li=[] for i in range(10000): li.append(i) def test2(): li=[i fo ...
- python描述符的应用
使用描述符为python实现类型检测 class Typed: def __get__(self, instance, owner): print(instance) print(owner) def ...
- SqlServer高版本数据库数据备份到低版本数据库上
想要将Sqlserver2014高版本备份的数据还原到低版本SqlServer2012上去,但是这在SqlServer中是没法直接还原数据库的,通过以下方法可以顺利还原. 通过高版本生成sql脚本在低 ...
- 20170416郑州市轻工业学院ACM校赛
这是个星期天,但是,这种非一线城市,重点城市什么的高中,放假从来不按套路出牌,几乎可以说能给你一天是福气.当然,比县里好的多,问在县里上高中的初中同学,放假更是比我们一天里的午休+晚上吃饭时间还要少. ...
- Redis2.8.7配置文件说明
Redis master配置文件说明 daemonize no 默认情况下,redis不是在后台运行的,如果需要在后台运行,把该项的值更改为yes daemonize yes # 当redis在后台运 ...
- String中的方法
1.string s1 = "abcdefghij"; string s2 = "kuo"; Console.WriteLine(s1.Clone()) ...
- Nand 的几个名词:oob,bbt,ecc
转:http://blog.csdn.net/lanmanck/article/details/4230904 例如Samsung K9F1208U0B,数据存储容量为64MB,采用块页式存储管理.8 ...