074 Search a 2D Matrix 搜索二维矩阵
编写一个高效的算法来搜索 m x n 矩阵中的一个目标值。该矩阵具有以下特性:
每行中的整数从左到右排序。
每行的第一个整数大于前一行的最后一个整数。
例如,
以下矩阵:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
给定 目标值= 3,返回 true。
详见:https://leetcode.com/problems/search-a-2d-matrix/description/
Java实现:
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int row=matrix.length-1;
int col=0;
while(row>=0&&col<matrix[0].length){
if(matrix[row][col]==target){
return true;
}else if(matrix[row][col]>target){
--row;
}else{
++col;
}
}
return false;
}
}
C++实现:
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int i = matrix.size()-1;
int j = 0;
while (i >=0 && j <matrix[0].size())
{
if (target > matrix[i][j])
{
++j;
}
else if (target < matrix[i][j])
{
--i;
}
else
{
return true;
}
}
return false;
}
};
参考:https://blog.csdn.net/zhangxiao93/article/details/49835511
074 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 ...
- 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 ...
- Leetcode74. Search a 2D Matrix搜索二维矩阵
编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 1: 输入: matrix ...
- 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 ...
- [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 74. 搜索二维矩阵(Search a 2D Matrix)
74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...
- LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37
240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...
随机推荐
- 三:JMS消息服务规范
一:JMS是什么?--->JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API--->用于在两个应用程 ...
- 白痴qwerta的胡言乱语(一句话日度感想?
10.2 >我tm吹爆这个Latex插件!!!太漂亮了吧?!!! 10.3 >什么鬼气考试 还有这考试的大家也肽水了吧 >再吹一次这个Latex插件!!! 10.6 >今天在琢 ...
- ASM认证与口令文件
ASM认证 ORACLE ASM 实例没有数据字典,所以连接ASM 实例只能通过如下三种系统权限来进行连接: SYSASM,SYSDBA,SYSOPER. 可以通过如下三种模式来连接ASM 实例:1. ...
- POJ1006Biorhythms——中国剩余定理
题目:http://poj.org/problem?id=1006 用扩展欧几里得算法求逆元,使用中国剩余定理: 本题较简单,可以手算直接写出,不过我仍使用了模板. 代码如下: #include< ...
- wpf datagrid row的命中测试
1. 添加鼠标左键处理 AddHandler(DataGrid.MouseLeftButtonDownEvent, new RoutedEventHandler(grdStudyList_MouseL ...
- MyEclipse修改Servlet模板
进入myeclipse的安装路径 然后进入plugins文件夹 打开搜索框,输入 *wizard* 找到名字是 com.genuitec.eclipse.wizards_11.5.0.me201310 ...
- 获取CPU ID ,disk ID, MAC ID (windows ARM linux Mac)
windows 命令行获取CPU ID,可以用ShellExecute wmic cpu get processorid ProcessorId BFEBFBFF000506E3 开源库: 查询CPU ...
- Maven查找添加方式
可以通过以下链接 : https://mvnrepository.com/
- 第二课2、ROS
1.ROS框架 分为以下三个级别: 1)文件系统级 2)计算图级 计算图级是ROS处理数据的一种点对点的网络形式,描述程序是如何运行的. 基本的计算图级概念包括:节点,参数服务器,消息,服务,主题和包 ...
- ASP.NET Core会议管理平台实战_4、参数校验、操作结果封装,注册参数配置
登陆和注册之前,需要封装 前端参数的校验,ajax的封装 参数校验,创建公共的类 ,它是一个静态类 这样在调用的时候,直接一句话就可以了,这就是封装的好处 空字符串的校验 调用方式 EF的源码里面有这 ...