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.该矩阵 ...
随机推荐
- WC2015 酱油记
这是真·酱油记! Day0 因为我们在上海,所以只要坐高铁就可以了2333.到了火车站以后我们坐大巴到学军中学恩,结果坐大巴的时间和做坐高铁的时间差不做←_←. 吐槽了一下住宿环境和课程表就已经晚上了 ...
- CentOs6.5下独立安装Nginx篇
一.检查系统是否安装了Nginx [root@localhost local]# find -name nginx [root@localhost local]# (如果已经安装了nginx就卸载掉原 ...
- OC面向对象—多态
OC面向对象—多态 一.基本概念 多态在代码中的体现,即为多种形态,必须要有继承,没有继承就没有多态. 在使用多态是,会进行动态检测,以调用真实的对象方法. 多态在代码中的体现即父类指针指向子类对象. ...
- JavaEE 7技术一览
参见此文: https://my.oschina.net/Barudisshu/blog/334903 perfect
- 2D 《触不可及》
这是一个有着特殊意义的夜晚(2014.9.19),孙红雷的电影这场电影<触不可及>也有着不一般的涵义,于我,在万达商场,遂留此一笔,仅供回忆.
- 关于mysql jsp字符编码的问题解决
1.对于post表单的jsp界面,要采用与数据库统一字符编码,在页头设置 <%@ page language="java" import="java.util.*& ...
- wine install 32bit netframewok
WINEARCH=win32 WINEPREFIX=~/.wine32 winecfg env WINEARCH=win32 WINEPREFIX=~/.wine32 winetricks dotne ...
- 如何利用Cloudera Manager来手动安装parcel包
1.问题的描述: 当你利用Cloudera Manager部署了CDH的集群后,也许随着你的业务需求,你需要对你的就去哪做一些优化,或者扩展之类的,这个时候你可能需要下载安装一些组件.例如,我最近在阅 ...
- Divisors
计算小于n的数中,约数个数最多的数,若有多个最输出最小的一个数. http://hihocoder.com/problemset/problem/1187 对于100有 60 = 2 * 2 * 3 ...
- 金浪网关ESR-6400G 告警解决办法
防火墙中提示为每秒新建连接数大于设定范围,自动断开改IP两分钟 解决办法:更换MAC地址与IP地址 下图是防火墙设置