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 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
, return true
.
假设直接对矩阵元素进行二分查找的话,时间复杂度是O(m*n),事实上非常easy想到先通过查找找到相应可能存在于哪一行,然后再在那行中查找是否存在,採用最简单的直接查找这样时间复杂度仅有O(m+n),假设这两次查找再分别採用二分查找的话,时间复杂度更能够减少到O(logm+logn),以下是O(m+n)的代码:
class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
if(matrix.empty())
return false;
int m = matrix.size();
int n = matrix[0].size();
int i = 0, j=0;
while(i<m && target>=matrix[i][0])
i++;
i--;
if(i==-1)
return false;
while(j<n)
{
if(target == matrix[i][j])
return true;
else
j++;
}
return false;
}
};
leetcode——Search a 2D Matrix 二维有序数组查找(AC)的更多相关文章
- [算法][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 搜索一个二维矩阵
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 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 解题报告
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 搜索二维矩阵
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(二分查找)
题意: 有一个矩阵,每行都有序,每行接在上一行尾后仍然有序.在此矩阵中查找是否存在某个数target. 思路: 这相当于用一个指针连续扫二维数组一样,一直p++就能到最后一个元素了.由于用vector ...
- [LeetCode] Search a 2D Matrix [25]
题目 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the fo ...
随机推荐
- float 保留两位小数
1.页面运算格式化数字 页面上有时候会用到数字的运算,运算过后会出现1.5999999999999这么长的数字,需要格式化数字,比如保留两位有效数字 首先导入这个标签 <%@ taglib ur ...
- Vijos P1680距离
题目 背景 简单的DP 描述 设有字符串X,我们称在X的头尾及中间插入任意多个空格后构成的新字符串为X的扩展串,如字符串X为”abcbcd”,则字符串“abcb_c_”,“_a_bcbcd_”和“ab ...
- C# 基础知识 (三).主子对话框数值传递
在C# winform编程中,我们经常会遇到不同窗口间需要传递数值的问题.比如数据库的应用,主窗口填写内容num1,点击按钮,在弹出的子窗口显示对应num1值;或者在子窗口填写新注册用户名信息,在主窗 ...
- JavaScript螺纹的问题和答案
要求: JavaScript是单线程的,有任务队列.比方使用setTimeou(func,secs)来在secs毫秒后向任务队列加入func.可是,setTimeout后面跟一个死循环,那么死循环导致 ...
- 【Eclipse】修改项目访问名称
Properties --> Web Project Settings --> Context root --> 输入想要用的名称(默认是项目名)
- Linux DM9000网卡驱动程序完全分析
Linux DM9000网卡驱动程序完全分析http://blog.csdn.net/ypoflyer/article/details/6209922
- [Swust OJ 1126]--神奇的矩阵(BFS,预处理,打表)
题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈 ...
- PHP学习笔记3-逻辑运算符
逻辑运算符图解: 逻辑且&&: <?php /** * Created by PhpStorm. * User: Administrator * Date: 2015/6/26 ...
- Java -- WeakHashMap
一.引言 Java中的引用类型由四种情况,强引用.软引用.弱引用.虚引用.关于这些的介绍可以参见鄙人另外一篇博文. ...
- linux下java窗口,正确显示中文
Tip1 1.在 JAVA_HOME/jre/lib/fonts/ 下建立个目录 fallback 2.在 fallback 里弄个中文字体最简单ln一下就好了 比如: ln -s /usr/shar ...