LeetCode Search a 2D Matrix(二分查找)
题意:
有一个矩阵,每行都有序,每行接在上一行尾后仍然有序。在此矩阵中查找是否存在某个数target。
思路:
这相当于用一个指针连续扫二维数组一样,一直p++就能到最后一个元素了。由于用vector装的,但是也是满足线性的。
二分:O(log n*m)
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target)
{
int n=matrix.size(), m=matrix[].size();
int L=, R=n*m-;
while(L<R)
{
int mid=L+(R-L+)/;
if(matrix[mid/m][mid%m]<=target) L=mid;
else R=mid-;
}
return matrix[L/m][L%m]==target;
}
};
AC代码
迭代:O(n+m)
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target)
{
int n=matrix.size(), m=matrix[].size();
int i=, j=m-;
while(i<n && j>=)
{
if(target==matrix[i][j]) return true;
if(target>matrix[i][j]) i++;
else j--;
}
return false;
}
};
AC代码
LeetCode Search a 2D Matrix(二分查找)的更多相关文章
- 74. Search a 2D Matrix(二分查找,剑指offer 1)
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 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 ...
- 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 ...
- 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 ...
随机推荐
- [整理]Svn常见问题汇总。
1.’.’ is not a working copy.Can’t open file‘.svn/entries’: 系统找不到指定的路径. 解答:原因是输入的访问路径不正确,如svn://192.1 ...
- 蓝桥杯历届试题 地宫取宝 dp or 记忆化搜索
问题描述 X 国王有一个地宫宝库.是 n x m 个格子的矩阵.每个格子放一件宝贝.每个宝贝贴着价值标签. 地宫的入口在左上角,出口在右下角. 小明被带到地宫的入口,国王要求他只能向右或向下行走. 走 ...
- 使用Join代替In
我们知道,在sql中使用IN让我们的where子句可以规定多个值.当需要从一个集合中查询包含某几个值的记录的时候,通常我们会选择使用IN来实现,其实,使用JOIN也可以实现这样的功能,而且性能要比IN ...
- Android Webview 背景透明
两个关键点: 1 fBarParams.format = PixelFormat.RGBA_8888; 2 mWebView.setBackgroundColor(Color.TRAN ...
- 一模 (3) day1
第一题: 题目大意:给出m个小于n的数,求出出现次数大于m div 2 的数. 1<=n<=2^31 1<=m<=10000 解题过程: 1.看到m的数据范围比较小,直接 ...
- discuz 系列产品 在ie9下注册成功后不跳转bug处理
header.htm 把 <meta http-equiv="x-ua-compatible" content="ie=7" /> 改为 <m ...
- [转载]android的消息处理机制(图+源码分析)——Looper,Handler,Message
2013-12-18 14:17:33 转载自: http://www.cnblogs.com/codingmyworld/archive/2011/09/14/2174255.html 请跳转到转载 ...
- IT公司100题-9-判断整数序列是不是二元查找树的后序遍历结果
问题描述: 输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果. 如果是返回true,否则返回false. 例如输入4, 8, 6, 12, 16, 14, 10,由于这一整数序列是如下树 ...
- Rhel6-haproxy+keepalived配置文档
系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.119:haproxy,keepalived server19.exa ...
- android 判断网络是否连接
package com.liucanwen.baidulocation.util; import android.app.Activity; import android.content.Contex ...