[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 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
.
74. Search a 2D Matrix 的变形,这题的矩阵特点是:每一行是按从左到右升序排列;每一列从上到下按升序排列。
解法:有特点的数是左下角和右上角的数。比如左下角的18开始,上面的数比它小,右边的数比它大,和目标数相比较,如果目标数大,就往右搜,如果目标数小,就往上搜。这样就可以判断目标数是否存在。或者从右上角15开始,左面的数比它小,下面的数比它大。
Python:
- class Solution:
- def searchMatrix(self, matrix, target):
- m = len(matrix)
- if m == 0:
- return False
- n = len(matrix[0])
- if n == 0:
- return False
- i, j = 0, n - 1
- while i < m and j >= 0:
- if matrix[i][j] == target:
- return True
- elif matrix[i][j] > target:
- j -= 1
- else:
- i += 1
- return False
C++:
- class Solution {
- public:
- bool searchMatrix(vector<vector<int> > &matrix, int target) {
- if (matrix.empty() || matrix[0].empty()) return false;
- if (target < matrix[0][0] || target > matrix.back().back()) return false;
- int x = matrix.size() - 1, y = 0;
- while (true) {
- if (matrix[x][y] > target) --x;
- else if (matrix[x][y] < target) ++y;
- else return true;
- if (x < 0 || y >= matrix[0].size()) break;
- }
- return false;
- }
- };
类似题目:
[LeetCode] 74. Search a 2D Matrix 搜索一个二维矩阵
All LeetCode Questions List 题目汇总
[LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II的更多相关文章
- [LeetCode] 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 ...
- [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] 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 ...
- Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)
1.问题描写叙述 写一个高效的算法.从一个m×n的整数矩阵中查找出给定的值,矩阵具有例如以下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 依据矩阵的特征非常 ...
- (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 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 ...
- 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_Medium tag: Binary Search
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- gcc的__builtin_函数(注意前面是两个下划线)
说明: GCC provides a large number of built-in functions other than the ones mentioned above. Some of t ...
- hak的使用
autohotkey简称ahk 它是一款轻量级的脚本语言文件,它可以干任何事情,如做dnf的连发脚本,类似按键精灵的自动化点击,按键自动打开文件一系列事情,文件需要按照ahk自己的语言,实现自定义的脚 ...
- HDU - 3535:AreYouBusy (分组背包)
题意:给你n个工作集合,给你T的时间去做它们.给你m和s,说明这个工作集合有m件事可以做,它们是s类的工作集合(s=0,1,2,s=0说明这m件事中最少得做一件,s=1说明这m件事中最多只能做一件,s ...
- Import declarations are not supported by current JavaScript version
原因为:不支持当前的js版本,在perference中进行设置javascript的版本即可 注意:在perference中进行更改,而不是defeaut perference,快捷键操作为:comm ...
- WebSocket 实现前后端通信的笔记
之前在做站内信时,用到了 WebSocket ,整理了一些笔记分享如下.本文基于 SpringBoot 2.1.5,本文不涉及环境搭建. 引入依赖 在 Spring 中要使用 WebSocket 功能 ...
- Eclipse 打包运行maven项目
https://www.cnblogs.com/tangshengwei/p/6341462.html
- 2019.12.06 java基础代码
操作系统中默认码表是:gbk (一个中文字符占两个字节): utf-8(一个中文字符占三个字节): 数据库建库时的默认码表是:拉丁码表: (1) public class 定义: ...
- webapp接口安全设计思路
在做webqq或者说app开发的时候,免不了会有接口是有权限的(如查询用户敏感信息等),这时接口安全设计思路就非常重要了. 简单一点,在APP中保存登录数据,每次调用接口时传输 程序员总能给自己找到偷 ...
- delphi调用https接口
delphi调用http接口直接使用idhttp就可以了,但是调用https接口的时候就需要和IdSSLIOHandlerSocket1控件一起使用. 截图中是两个控件的具体配置,需要注意的是IdSS ...
- hive集成kerberos
1.票据的生成 kdc服务器操作,生成用于hive身份验证的principal 1.1.创建principal # kadmin.local -q “addprinc -randkey hive/yj ...