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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)

    1.问题描写叙述 写一个高效的算法.从一个m×n的整数矩阵中查找出给定的值,矩阵具有例如以下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 依据矩阵的特征非常 ...

  6. (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 ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. Jenkins+robotframework单机版简约教程

    迫于某人极渴望学自动化测试,因此写下此简约教程.妈蛋我是个JAVA后端开发啊... 此教程为基于window系统的Jenkins单机版,测试代码无版本控制的精要压缩版本教程,勿喷 前提:通过Jenki ...

  2. 性能测试基础---URL和HTTP协议

    ·URL和HTTP协议: ·URL构成: URL是web应用进行资源访问的主要方式.一般来说,由五个部分构成: 示例:http://192.168.2.212/phpwind1/searcher.ph ...

  3. Python - 2和3的区别

    编码: Python2的默认编码是ASCII码,这是导致Python2中经常遇到编码问题的主要原因之一,至于原因,在于Python这门语言出现的时候,还没有Unicode! Python3默认编码是U ...

  4. 当电脑上有两个mysql时,如何让jmeter连接自己需要的那个mysql

    1.当有两个mysql时,修改其中一个的mysql端口号为3307 ,也可以是其他8788, 在文件my.ini中修改端口号为:3307:修改完之后记得重启mysql哦:dos下命令可以用net st ...

  5. Ubuntu创建启动器(快捷方式)的方式

    解压.tar.gz的navicat之后发现不能自动生成启动器了,研究了一下发现: 虽然不少带有图形界面的程序会在安装时自动在桌面上创建快捷方式,还有一些图形界面程序或者命令行程序可能需要你手动创建快捷 ...

  6. ARTS-week3

    Algorithm 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度.不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件 ...

  7. assert 断言

    输入 assert 1>2,'123' 输出结果 assert 1>2,'123' AssertionError: 123

  8. [USACO08OCT]:打井Watering Hole(MST)

    题意:有N个牧场,每个牧场修水井花费Wi,连接牧场花费Pij,问最小花费,使得每个牧场要么有水井,要么和有水井的牧场有通道. 思路:加一个格外的节点O,连接O表示修井,边权是修井的费用.     那么 ...

  9. SSH——ssh_exchange_identification: read: Connection reset by peer

    前言 ssh远程连接出错 步骤 查看ssh的详细信息 [root@pre-nginx02 ~]# ssh -v 192.168.1.164 OpenSSH_6.6.1, OpenSSL 1.0.1e- ...

  10. MySQL 日期格式化,取年月日等相关操作

    日期取年.月.日 select id, phone,time,year(time),month(time), DAY(time),TIME(time) from user where phone='x ...