【刷题-LeetCode】240. Search a 2D Matrix II
- Search a 2D Matrix II
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 in ascending from left to right.
- Integers in each column are sorted in ascending from top to bottom.
Example:
Consider the following matrix:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.
Given target = 20, return false.
解 从右上角到左下角,往左搜索变小,往下搜索变大,假设在位置 i, j处的元素值为n,搜索过程为:
- target == n :找到,结束
- target > n:往下走,增加n
- target < n:往左走,降低n
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();
int i = 0, j = n-1;
while(i < m && j >= 0){
if(matrix[i][j] == target)return true;
if(matrix[i][j] > target){
j--;
}else{
i++;
}
}
return false;
}
};
【刷题-LeetCode】240. Search a 2D Matrix II的更多相关文章
- [LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- (medium)LeetCode 240.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 240. 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 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)
1.问题描写叙述 写一个高效的算法.从一个m×n的整数矩阵中查找出给定的值,矩阵具有例如以下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 依据矩阵的特征非常 ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- 【LeetCode】240. Search a 2D Matrix II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...
- 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【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] 240. Search a 2D Matrix II_Medium tag: Binary Search
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- AT1445 乱数生成 题解
Description 有一个机器会等概率从 \(1\) 到 \(n\) 的正整数中选出一个整数.显然地,这个机器运行 \(3\) 次后会得到 \(3\) 个整数.求这 \(3\) 个整数的中位数是 ...
- java 编程基础 反射方式获取泛型的类型Fileld.getGenericType() 或Method.getGenericParameterTypes(); (ParameterizedType) ;getActualTypeArguments()
引言 自从JDK5以后,Java Class类增加了泛型功能,从而允许使用泛型来限制Class类,例如,String.class的类型实际上是 Class 如果 Class 对应的类暂时未知,则使 C ...
- Pointcut 表达式
AOP 概念篇 今天介绍 Pointcut 的表达式 通配符 常见的通配符如下 .. 含义一:方法表达式中.代表任意数量的参数 @Service public class HelloService { ...
- lldb调试C++总结(2)
lldb help 可能你会忘记某些指令的用法, 使用help可以帮助你. (lldb) breakpoint --help invalid command 'breakpoint --help'. ...
- C1. 组队活动 Small(BNUOJ)
C1. 组队活动 Small Time Limit: 1000ms Memory Limit: 131072KB 64-bit integer IO format: %lld Java cl ...
- Java学到什么程度可以面试工作?
先说结论: 1 大多数公司,对于Java初级开发的要求是,会用Spring Boot+JPA做增删改查 2 所以零基础的Java小白,无需学太多的内容,只要掌握Spring Boot+JPA做增删改 ...
- 『学了就忘』vim编辑器基础 — 97、vim使用技巧
目录 1.在vim中导入其他文件内容或命令结果 (1)导入其他文件内容 (2)在vim中执行系统命令 (3)导入命令结果 2.设定快捷键 3.字符替换 4.多文件打开 vim使用技巧,就是vim编辑器 ...
- 13 个 C# 10 特性
原文链接:https://blog.okyrylchuk.dev 原文作者:Oleg Kyrylchuk 译: 等天黑 常量的内插字符串 C# 10 允许使用在常量字符串初始化中使用插值, 如下 co ...
- Xcode Error returned in reply: Connection invalid
当电脑上有Xcode两个版本是使用其中的某一个的Simulator时出现了这样的报错 在Preference->Location->Command Line Tool 选择 Xcode 9 ...
- CS5213高性价比替代AG6200芯片|兼容台湾AG6200芯片|CS5213Capstone
CS5213是一款HDMI转VGA带音频信号转出的芯片方案,CS5213支持HDCP协议,且外围电路比台湾安格AG6200要少,且本身CS5213芯片成本比AG6200要低,整个方案设计简单性价比较高 ...