[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 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.
For 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
.
思路:
由于行和列都是有序的,所以我们可以从二维数组的右上角开始搜索。如果值刚好等于target,则返回;如果值小于target,则往下移动;否则往左移动。假设二维数组的行个数和列个数分别是m和n,则时间复杂度是O(m+n)。空间复杂度是O(1)。
代码:
class Solution {
public:bool searchMatrix(vector<vector<int>>& matrix, int target) {if (matrix.size() == 0 || matrix[0].size() == 0) {return false;}int row_num = matrix.size(), col_num = matrix[0].size();int row = 0, col = col_num - 1;while (row < row_num && col >= 0) {if (matrix[row][col] == target) {return true;}else if (matrix[row][col] < target) {++row;}else {--col;}}return false;}
};
[Leetcode] 240. Search a 2D Matrix II 解题报告相关推荐
- LeetCode 240. Search a 2D Matrix II
LeetCode 240. Search a 2D Matrix II Solution1: 为什么把第74题的代码改都不用改的拿过来就可以AC,一脸懵逼啊... class Solution { p ...
- leetcode 240. Search a 2D Matrix II | 240. 搜索二维矩阵 II(Java)
题目 https://leetcode.com/problems/search-a-2d-matrix-ii/ 题解 方法1 思路类似于 leetcode 200. Number of Islands ...
- Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)
1.问题描述 写一个高效的算法,从一个 m×n m\times n的整数矩阵中查找出给定的值,矩阵具有如下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 根据 ...
- LeetCode 74. Search a 2D Matrix
LeetCode 74. Search a 2D Matrix Solution1:我的答案 <剑指offer>原题 class Solution { public:bool search ...
- LeetCode Search a 2D Matrix II
问题:给出一个二维数组,其行,列是递增的,和一个要查找的数,问矩阵中是否有要查找的数 思路: 第一种方法是直接暴力查找,在二维数组中查找. 第二种方法是基于对角线上,在行,列上作二分查找 第三种方法递 ...
- [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 f ...
- 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 ...
- [Leetcode] 33. Search in Rotated Sorted Array 解题报告
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- 【LeetCode】487. Max Consecutive Ones II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...
最新文章
- YOLO在升级 | PP-YOLO v2开源致敬YOLOV4携带Tricks又准又快地归来(附论文与源码)...
- iOS socket
- Linux查看进程和终止进程的技巧
- Zmodem transfer canceled by remote side
- C语言试题二十四之编写一个函数unsigned function(unsigned w),w使一个大于10的无符号整数,若w是n(n≥2)位的整数,则函数求出w后n-1位的数作为函数值返回。
- C语言程序判断计算机的CPU大小端
- mac安装python环境教程_python mac下安装虚拟环境的图文教程
- 谈谈如何在面试中发掘程序猿的核心竞争力 什么是程序员的核心竞争力?
- 【Spring】Spring 父子容器
- 【报告分享】2021年人工智能安全风险及治理研究报告.pdf(附下载链接)
- JDK5后的特性整理
- 02. Prefer consts, enums, and inlines to #defines
- vim如何删除^M字符
- Qt面对高分辨率屏幕的解决方法思考
- 马知恩周义仓编常微分方程定性与稳定性方法部分习题参考解答
- 2023年最新微信记账小程序源码+简约大气
- 歹徒逃亡3——————刺杀行动
- 2021-10-01
- 为什么引入非线性激励函数
- Linux如何设置时区、时间