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.该矩阵 ...
随机推荐
- ListView在列表的头部和底部添加布局——addHeaderView,addFooterView
addHeaderView()方法:主要是向listView的头部添加布局addFooterView()方法:主要是向listView的底部添加布局 以addHeaderView为例: View he ...
- Oracle 字符串分割排序冒泡算法
例子: 一个字符串"11,15,13,17,12",以逗号分割,现在要排序成"11,12,13,15,17". 写了一个实现方法,记录下来以备后用: ----- ...
- Java:Map
接口Map<K,V>,K – 映射所维护的键的类型:V – 映射值的类型. public interface Map<K,V>,将键映射到值的对象.一个映射不能包括重复的键:每 ...
- UE3植被工具-支持刷Actor)
[目标] 植被工具-刷Actor [思路] 1 添加类型FFoliageMeshInfo.AddInstance 的函数 2 添加Instance就直接SpawnActor 3 类结构 4 修改的函数 ...
- MVN使用随笔
001 创建项目 mvn archetype:generate -DgroupId=com.company.push.monitor -DartifactId=push-monitor -Darche ...
- 【USB多路电源】---需求分析方案制定
需求描述: USB接口输入5V,分别输出±5V,100mA; 3.3V,100mA: 1.2V,500mA:四路电源.同时可给锂电池充电,在移除USB输入时锂电池能供电. 分析: 首先考虑需要一个充电 ...
- xxxxxxxxx
异步for (var index = 0; index < data.length; index++) { var req = http.request(urlEntity, function( ...
- Angular - ng-repeat高级用法
ng-repeat高级用法: 遍历数组: <li ng-repeat="item in array">{{item}}</li> 遍历对象: k ...
- android recyclerview 更新ui
http://blog.csdn.net/leejizhou/article/details/51179233
- poj2387 spfa求最短路
//Accepted 4688 KB 63 ms #include <cstdio> #include <cstring> #include <iostream> ...