当前位置:网站首页>240. 搜索二维矩阵 II
240. 搜索二维矩阵 II
2022-07-29 20:16:00 【小卢要刷力扣题】
前言
编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target 。该矩阵具有以下特性:
每行的元素从左到右升序排列。
每列的元素从上到下升序排列。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/search-a-2d-matrix-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路
经典解法: .
从右上角开始找
如果当前的数大于目标
则从左边找
如果当前的数小于目标数则往下找
代码
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int row=0;
int col=matrix[0].length-1;
while(row<matrix.length&&col>=0){
if(matrix[row][col]==target){
return true;
}else if(matrix[row][col]>target){
col--;
}else{
row++;
}
}
return false;
}
}
边栏推荐
- 8.2实训任务 Sqoop的安装与配置
- Agile Organization | The path for enterprises to overcome the impact of the digital wave
- 震荡波病毒原代码(勒索病毒源代码)
- Chrome——插件推荐
- LeetCode 0144. 二叉树的前序遍历:二叉树必会题
- 尿素偶联Urea-siRNA Conjugates|Cyclodextrin-siRNA-β-CD环糊精修饰RNA核酸(解析说明)
- 磁性层状双金属氢氧化物和酶-DNA复合物|聚乙烯亚胺-DNA复合物(PEI/DNA)|作用机理
- Kubernetes:(四)常用命令
- ES6用法,面试大全
- EasyExce template filling generation of Excel of actual operation, many processing sheet page
猜你喜欢

图床软件要收费,算了我自己写一个开源免费的。

RNA修饰技术介绍|介孔二氧化硅纳米颗粒(MSN)搭载的微小RNA-24(miR-24)纳米载体复合物

Safe Browser will have these hidden features that will let you play around with your browser

ds1302——Bin brother 51

mos管闩锁效应理解学习

LeetCode 0144. 二叉树的前序遍历:二叉树必会题

从实例学Kettle(一):获取股票行情数据

JUC并发编程基础AQS

正则表达式

Omni-channel e-commerce | How can well-known domestic cosmeceuticals seize the opportunity to achieve rapid growth?
随机推荐
Kubernetes:(四)常用命令
Agile Organization | The path for enterprises to overcome the impact of the digital wave
Chrome——插件推荐
叶酸&适配体修饰DNA纳米载体|CdS纳米颗粒修饰DNA|科研试剂
include用法及搭配(include相关短语)
The difference between analog, digital and switching
LeetCode_474_ one and zero
The younger brother asked: Is the work of a programmer a day’s work of code?
全排列的一点小技巧:康托展开
offsetwidth111[easy to understand]
es6语法使用默认参数和解构
Internship: use easypoi to import and export excel table data
LOG4J 学习
RNA修饰技术介绍|介孔二氧化硅纳米颗粒(MSN)搭载的微小RNA-24(miR-24)纳米载体复合物
[GXYCTF2019] ban dolls
Samba服务器配置(什么情况下需要服务器)
如何进入董事会:给CIO的十条建议
R language for airbnb data nlp text mining, geography, word cloud visualization, regression GAM model, cross-validation analysis
scratch programming + elementary math
回归——岭回归