【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坐标,% ...
随机推荐
- WCF--安全小见解...
由于WCF写的服务需要Ajax来进行调用(这个配置过程也是一个比较咕~~(╯﹏╰)b的经历...), 所以调用的过程都是前台可以看到的,不加点安全措施上去,真的像是一个裸奔在互联网上的接口... 反正 ...
- etcd
https://github.com/silenceper/dcmp http://studygolang.com/topics/1866
- IBM:领导力素质的三环模式
文章来源:http://blog.vsharing.com/sdtcj/A1826934.html 在翰威特公司于2003年评选的美国企业“领导人才最佳雇主”名单上,IBM名列榜首.而纽约< ...
- Linux系统安装LAMP
说明: 系统版本:Ubuntu14.04-LTS,可以在Ubuntu官网直接下载.Ubuntu其他版本也可安装本方法搭建LAMP环境! 步骤一,安装apache2 1 sudo apt-get ins ...
- <s:url>指向的Action只执行一次,清除浏览器缓存文件后又可执行一次。
Action中的方法仅为静态变量赋值,而其他访问数据库的Action可以被重复执行. 起初判断可能是静态变量的内存机制导致不能重复执行. 然后发现清楚浏览器缓存文件后又可以执行一次了,看来原因在Jsp ...
- Swift语法入门
正文参考: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Progra ...
- VS2013 Community配置OpenCV3.0.0
配置环境:32位win7系统+VS2013 Community版本 1.首先从OpenCV官网上下载最新版本的OpenCV for Windows. 2.直接双击打开下载得到的opencv-3.0.0 ...
- Oracle 中循环遍历某张表,并对符合条件的进行Update操作
BEGIN FOR L_RECORD IN (select RECORD_ID,CURR_PERIOD,PERIOD_START_DATE, (sysdate- PERIOD_START_DATE) ...
- BZOJ1500——维修序列
动态的最大子段和 就是splay啊,说一下GSS1吧,维护四个值,一个是这个区间和(下面说sum), 一个是从左边开始的最大和(下面说ls)和右边开始的最大和(下面说rs), 还有一个就是最大区间和( ...
- [COJ0985]WZJ的数据结构(负十五)
[COJ0985]WZJ的数据结构(负十五) 试题描述 CHX有一个问题想问问大家.给你一个长度为N的数列A,请你找到两个位置L,R,使得A[L].A[L+1].…….A[R]中没有重复的数,输出R- ...