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 ...
随机推荐
- win7_oracle11g_64位连接32位PLSQL_Developer
工具/原料 已经装好的64位Oracle数据库 window7_64位的操作系统 PLSQL_Developer 9.0以上版本(目前只有32位的):下面有下载连接! 官方的 instantcli ...
- 工程技巧Linux上建立工程项目
程序中用到的核心代码用库的形式进行封装,并且给出示例程序,下面给出一个程序文件夹的建立脚本. 如运行sh MakeProject.sh PersonNameIdentification PNILib ...
- Ubuntu 设置su密码
如果之前安装时没有设置root密码,可以如下设置: 命令窗口中输入:sudo passwd [sudo] password for 用户名: 这里输入你sudo 的密码输入新的 UNIX 密码: 重 ...
- CPU信息查询
cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l //查看CPU的个数 cat /proc/cpuinfo |grep ...
- 转: Oracle中的物化视图
物化视图创建语法:CREATE MATERIALIZED VIEW <schema.name>PCTFREE <integer>--存储参数PCTUSED <intege ...
- Oracle连接查询内容整理
--内连接--select t.*,b.bumenmc from T_HQ_RYXX t,t_hq_bm b where t.bum = b.bumenbm--select * from t_hq_r ...
- flash as3 socket安全服务网关(socket policy file server)
关键字: SecurityErrorEvent socket as3 flash有着自己的一套安全处理模式,在socket方面,我这样的菜鸟无法理解他的好处:一句话,不怀好意的人如果想用flash写一 ...
- 周游加拿大(dp好题)
你赢得了一场航空公司举办的比赛,奖品是一张加拿大环游机票.旅行在这家航空公司开放的最西边的城市开始,然后一直自西向东旅行,直到你到达最东边的城市,再由东向西返回,直到你回到开始的城市.每个城市只能访问 ...
- wScratchPad 实现刮刮卡效果
插件网址http://wscratchpad.websanova.com/
- JSON:org.json的基本用法
java中用于解释json的主流工具有org.json.json-lib与gson,本文介绍org.json的应用. 官方文档: http://www.json.org/java/ http://de ...