[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 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.
解题思路
二维数组中的查找,这是个简单的题,依据题意能够推出,这个二维数组事实上是一个有序的一维数组。解决思路也非常easy想到,每次比較每一维最后一个元素,假设该元素比要找的元素小,说明这个行不可能含该元素;假设相等,那就找到了,假设最后一个元素比要找元素大,说明该元素假设出现比在这一行。然后再在这一行中即可查找(能够用顺序,也可二分)。
代码实现
class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
int m = matrix.size();
if(m<=0) return false;
int n = matrix[0].size();
for(int i=0; i<m; ++i){
if(matrix[i][n-1] == target)
return true;
else if(matrix[i][n-1] < target)
continue;
else{
for(int j=0; j<n; ++j)
if(matrix[i][j] == target)
return true;
return false;
}
}
return false;
}
};
另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
)
[LeetCode] Search a 2D Matrix [25]的更多相关文章
- [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 搜索一个二维矩阵
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 follo ...
- [leetcode]Search a 2D Matrix @ Python
原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/ 题意: Write an efficient algorithm that sear ...
- [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 二分搜索
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- perl /m修饰符使用说明
高级用法: 多行匹配: grok正则和普通正则一样, 默认是不支持匹配回车换行的. perl的/m选项 The /m modifier allows ^ and $ to match immediat ...
- 习题3.15 自调整表Find例程
#include<stdio.h> #include<stdlib.h> typedef int * List; /* 自调整表的Find数组实现 */ int Find(Li ...
- 常见 wifi热点的linux 驱动
小度Wifi.360Wifi Windows.linux驱动 小度wifi什么的就是一个无线网卡,当然可以自由使用,然官方却说不支持无限网卡功能… 现提供Windows平台和linux平台的驱动安装方 ...
- iOS中UITextView键盘回收
iOS开发中,发现UITextView没有像UITextField中textFieldShouldReturn:这样的方法,那么要实现UITextView关闭键盘,就必须使用其他的方法,下面是可以使用 ...
- OC中ARC forbids explicit message send of release错误(转)
ARC forbids explicit message send of'release' 很显然,是ARC的问题. 错误原因:在创建工程的时候点选了“Use Automatic Reference ...
- 固定cell.imageView.image的大小
cell.imageView.image的大小 会随着Cell的高度而变化,不同的图片显示的也不一样,在网上找了几种方法,简单方便的是下面这种: UIImage *icon = [UIImage im ...
- Cocos2dx 中的CCCallFunc,CCCallFuncN,CCCallFuncND,CCCallFuncO比较
qinning199原创,欢迎转载.转载请注明:http://www.cocos2dx.net/?p=27 首先看一张图片,表示了CCCallFunc的继承关系: 1.CCCallFunc,回调,调用 ...
- session相关----高手请跳过!
session["username"]=null;//Session.Remove("username");的结果是session["username ...
- C# DataTable 详解
添加引用 using System.Data; 创建表 //创建一个空表 DataTable dt = new DataTable(); //创建一个名为"Table_New"的空 ...
- Request和Response详解
转自:http://zhidao.baidu.com/link?url=8BI0cjlcFdBSJKHTZlpo874eqtbTJoZfrh3miQgM_05RvSER8skPiBc1wSPZtXT8 ...