【leetcode】 Search a 2D Matrix (easy)
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.
实际上就是二分搜索, 不难, 一次AC。
注意一下退出条件是 l <= r 就行。有等于。
class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
if(matrix.empty())
return false; int length = matrix.size() * matrix[].size();
int l = , r = length - ; while(l <= r) //注意 这里包含等于
{
int mid = (l + r) / ;
int col = mid % matrix[].size();
int row = mid / matrix[].size();
if(matrix[row][col] < target)
{
l = mid + ;
}
else if(matrix[row][col] > target)
{
r = mid - ;
}
else
{
return true;
} }
return false;
}
};
【leetcode】 Search a 2D Matrix (easy)的更多相关文章
- 【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】【Medium】Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- 【数组】Search a 2D Matrix
题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...
- [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 ...
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- [Leetcode Week13]Search a 2D Matrix
Search a 2D Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/search-a-2d-matrix/description/ D ...
- [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】Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
随机推荐
- springMVC 缓存(入门 spring+mybaties+redis一)
使用redis之前需要咋电脑上安装redis: 使用spring+mybaties+redis的本质是扩展类 org.apache.ibatis.cache.Cache:在我们自己扩展的Cache ...
- 清北学堂模拟day4 传球接力
[问题描述]n 个小朋友在玩传球. 小朋友们用 1 到 n 的正整数编号. 每个小朋友有一个固定的传球对象,第 i 个小朋友在接到球后会将球传给第 ai个小朋友, 并且第 i 个小朋友与第 ai个小朋 ...
- C#3.0 特性
C#3.0特性 隐式类型的本地变量和数组 对象初始值设定项 集合初始值设定项 扩展方法 匿名类型 lambda表达式 查询关键字 自动实现的属性 分布方法定义 lambda表达式与表达式树 https ...
- Ubuntu 12 修改环境变量
Ubuntu Linux系统包含两类环境变量:系统环境变量和用户环境变量.系统环境变量对所有系统用户都有效,用户环境变量仅仅对当前的用户有效. 修改用户环境变量 用户环境变量通常被存储在下面的文件中: ...
- post 之checkbox 未被选中解决方法
第一种方法: http://cnn237111.blog.51cto.com/2359144/1293812 第二种方法(推荐): http://blog.csdn.net/xyanghomepage ...
- hash-6.CopyOnWriteArrayList
1.ArrayList的add方法 public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount ...
- 怎样用路由器共享需要网页认证的wifi
设置步骤:第一步:登录管理界面 1.连接电脑使用单机能上网的电脑,通过网线连接到路由器的LAN口.2.登录管理界面打开电脑的浏览器,清空地址栏后,输入路由器的管理地址(以路由器底部标贴标识的管理地址为 ...
- JavaScript的面临的9个陷阱
虽然不是什么很高深的技术问题,但注意一下,会使您的编程轻松些,即所谓make life easier. 笔者对某些陷阱会混杂一些评点. 1. 最后一个逗号 如这段代码,注意最后一个逗号,按语言学角 ...
- iOS 不规则的ImageView
http://blog.csdn.net/kevinpake/article/details/41205715 我们在做iOS开发的时候,往往需要实现不规则形状的头像,如: 那如何去实现? 通常图片都 ...
- python 字典 注意点
dict()构造函数直接从键-值对序列创建字典: >>> >>> dict([('sape', 4139), ('guido', 4127), ('jack', 4 ...