Leetcode74. Search a 2D Matrix搜索二维矩阵
编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性:
- 每行中的整数从左到右按升序排列。
- 每行的第一个整数大于前一行的最后一个整数。
示例 1:
输入: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 3 输出: true
示例 2:
输入: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 13 输出: false
写法一:
class Solution {
public:
bool searchMatrix(vector<vector<int> >& matrix, int target)
{
int r = matrix.size();
if(r == 0)
return false;
int c = matrix[0].size();
if(c == 0)
return false;
int i = 0;
int j = c - 1;
for(; i < r; i++)
{
if(target <= matrix[i][j])
break;
}
if(i >= r)
return false;
for(; j >= 0; j--)
{
if(target == matrix[i][j])
return true;
}
if(j < 0)
return false;
}
};
写法二:
class Solution {
public:
bool searchMatrix(vector<vector<int> >& matrix, int target)
{
int r = matrix.size();
if(r == 0)
return false;
int c = matrix[0].size();
if(c == 0)
return false;
int i = 0;
int j = c - 1;
while(i >= 0 && i < r && j >= 0 && j < c)
{
if(matrix[i][j] == target)
return true;
else if(matrix[i][j] < target)
i++;
else
j--;
}
return false;
}
};
Leetcode74. Search a 2D Matrix搜索二维矩阵的更多相关文章
- [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 ...
- 074 Search a 2D Matrix 搜索二维矩阵
编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性: 每行中的整数从左到右排序. 每行的第一个整数大于前一行的最后一个整数.例如,以下矩阵:[ [1, 3, ...
- 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 ...
- 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 OJ: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)
74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...
- [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之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37
240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...
随机推荐
- 19-11-12-Aftern-℘
我饿死了,于是写写博客安慰一下即将退役的自己. ZJ: T1. 三种颜色,想到一道神奇的‘天空龙’. 于是觉得此题可做. 那好了. 于是切掉,还拿了一个暴力对拍.疯狂A. 啊dfs慢的要死了 T2一眼 ...
- Ajax4Jsf 简单介绍
Ajax4jsf 允许开发人员将 Ajax 功能添加到 JSF 应用程序中,而不需要 JavaScript 或用 Ajax 图形部件替换现有的组件.这个包还允许在使用 Java 2D 库时动态地生成图 ...
- Android基础控件ToggleButton和Switch开关按钮
1.简介 ToggleButton和Switch都是开关按钮,只不过Switch要Android4.0之后才能使用! ToggleButton <!--checked 是否选择--> &l ...
- webservice技术--服务器端
1.webservice实现单点登录具体逻辑为 ①软通web端作为客户端,请求wi社区后台,进行登录请求 ②wi社区后台验证t,核实无误后,走登录逻辑,直接进入欢迎页 ③如果有错误,封装错误xml,返 ...
- C#获取七牛云token/删除七牛云图片接口
// 获取七牛token public ApiResponse GetQiniuToken(QiniuToken req) { try { Mac mac = new Mac(req.AccessKe ...
- Konig定理及证明
Konig定理 由匈牙利数学家柯尼希(D.Konig)于1913年首先陈述的定理. 定理的内容:在0-1矩阵中,1的最大独立集合最小覆盖包含的元素个数相同,等价地,二分图中的最大匹配数等于这个图中的最 ...
- CentOS使用rpm离线安装mariadb
本文不再更新,可能存在内容过时的情况,实时更新请移步原文地址:CentOS使用rpm离线安装mariadb: 环境: CentOS Linux release 7.6.1810 (Core) mari ...
- ODOO 新API修饰符
Odoo8中,API接口分为traditaional style和record style两种类型: traditional style指的就是我们在7.0中使用的类型,def(self,cr,uid ...
- ALS算法实现用户音乐打分预测
很多人在决定是否看一部电影之前都会去豆瓣看下评分作为参考,看完电影也会给一个自己的分数.每个人对每个商品或者电影或是音乐都有一个心理的分数,这个分数标明用户是否对这个内容满意.作为内容的提供方,如果可 ...
- Java的跨平台特性
语言跨平台是编译后的文件跨平台,而不是源程序跨平台.Java源代码首先经过编译器生成字节码,即class文件,该class文件与平台无关,而class文件经过解释执行之后翻译成最终的机器码,这是平台相 ...