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.该矩阵具有以下特性 ...
随机推荐
- selenium中类名不能与方法名相同
不要将selenium中的类名命名成需要用到的方法名,不然会报错!
- Speaking 1
What clothes do you usually like to wear?Well I like fashionable clothes, but I also want to be comf ...
- C字符串末尾的'\0'问题
C语言的字符串要注意最后一位默认是'/0'的问题.这是一个易错点. strlen()计算长度时不考虑末尾的'\0' //例1 void test1() { ]; "; strcpy( str ...
- C++中两个类相互包含引用问题
在构造自己的类时,有可能会碰到两个类之间的相互引用问题,例如:定义了类A类B,A中使用了B定义的类型,B中也使用了A定义的类型 class A { int i; B b; } class B { in ...
- Web 攻击之 XSS 攻击及防御策略
XSS 攻击 介绍 XSS 攻击,从最初 netscap 推出 javascript 时,就已经察觉到了危险. 我们常常需要面临跨域的解决方案,其实同源策略是保护我们的网站.糟糕的跨域会带来危险,虽然 ...
- python 案例之老王开枪
- 乱写的一个SQL框架
闲来没事,看了mybatis的实现形式,就心血来潮的自己弄了一个仿照mybatis的框架,性能肯定不好,而且有很多问题,但是是一次有益的尝试 1.基本配置文件 <!--加载数据源--> & ...
- 组播基本概念、IGMP、IGMP监听学习笔记
前言 一直对组播这个概念迷迷糊糊,特别是交换机处理组播的方式,非常想搞懂但是懒癌发作.这几天终于耐心地看了下有关组播的资料,大致了解了一下同一广播域内组播的相关知识.组播占了计算机网络的一大部分,特别 ...
- fabric优先级,进程管理
fabric在执行一些命令或者脚本的时候,会执行了操作,但是,进程启动失败,google,发现fabric在执行脚本或者进程的时候,加入set -m参数,就可以正常运行了,解释是:"set ...
- ubuntu 修改资源镜像
要修改的文件 /etc/apt/sources.list 原资源镜像文件 deb http://mirrors.aliyun.com/ubuntu/ yakkety main multiverse r ...