search-a-2d-matrix(二维矩阵查找)
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, returntrue.
class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
int m = matrix.size();
int n = matrix[0].size();
int c = n-1;
for(int r = 0;(r<m)&&(c>=0);)
{
if(matrix[r][c]>target)
{
c--;
}
else if(matrix[r][c]<target)
{
r++;
}
else
return true;
}
return false;
}
//每次比较行末,由于已经排序好,可以进行行列删除
};

search-a-2d-matrix(二维矩阵查找)的更多相关文章
- Search a 2D Matrix,在有序矩阵查找,二分查找的变形; 行有序,列有序查找。
问题描述:矩阵每一行有序,每一行的最后一个元素小于下一行的第一个元素,查找. 算法分析:这样的矩阵其实就是一个有序序列,可以使用折半查找算法. public class SearchInSortedM ...
- [算法][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] 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 a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵
11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a 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之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
随机推荐
- C#泛型List的用法
C#泛型List的用法 来源:C#学习 发布时间:2014/1/4 一.List<T>命名空间: System.Collections.Generic(程序集:mscorlib) 二 ...
- Linux 学会这些基本可以啦
1,Linux 内壳文件:cat /etc/issue OR /etc/redhat-release ubuntu[apt-get install -y vim] centos[yum install ...
- cloud theory is a failure? 分类: Cloud Computing 2013-12-26 06:52 269人阅读 评论(0) 收藏
since LTE came out, with thin client cloud computing and broadband communication clouding 不攻自破了.but ...
- android通知-Notification
android中,当app需要向发送一些通知,让使用者注意到你想要告知的信息时,可以用Notification.下面,就来讨论一下,Notification的用法,我们从实际的小例子来进行学习. 1. ...
- jetty启动不能保存
主要原因是jetty缓存的静态页面不能被修改.只需要在web.xml文件中配置如下: <servlet> <!-- Override init parameter to avo ...
- Bootstrap框架基础
特点:写非常少的代码 即可实现多终端的页面适配 ☑ 简单灵活可用于架构流行的用户界面和交互接口的html.css.javascript工具集. ☑ 基于html5.css3的bootstrap,具 ...
- windows核心编程---第三章 内核对象及句柄本质
本章讨论的是相对抽象的概念,不涉及任何具体的内核对象的细节而是讨论所有内核对象的共有特性. 首先让我们来了解一下什么是内核对象.内核对象通过API来创建,每个内核对象是一个数据结构,它对应一块内存 ...
- js 点击复制内容
<textarea id="pushUrlsTxt" rows="5" cols="55"></textarea> ...
- 【Selenium2+Python】定位
定位Frame driver.switch_to_frame("frameID") 多窗口切换 #获得当前窗口 nowhandle = driver.current_window_ ...
- 处理oracle的死锁
Oracle数据库操作中,我们有时会用到锁表查询以及解锁和kill进程等操作,那么这些操作是怎么实现的呢?本文我们主要就介绍一下这部分内容.(1)锁表查询的代码有以下的形式:select count( ...