编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target。该矩阵具有以下特性:

  • 每行的元素从左到右升序排列。
  • 每列的元素从上到下升序排列。

示例:

现有矩阵 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] ]

给定 target = 5,返回 true。

给定 target = 20,返回 false。

如果从左上角开始,则往右往下都是递增,不能判定到底往哪个方向。

但是如果是右上角或者左下角,则两个方向情况都不同,就能明确的判定是哪种情况了

class Solution {
public:
bool searchMatrix(vector<vector<int> >& matrix, int target)
{
int r = matrix.size();
if(r == 0)
return false;
int c = matrix[0].size();
int j = c - 1;
int i = 0;
while(i >= 0 && i < r && j >= 0 && j < c)
{
if(matrix[i][j] == target)
{
return true;
}
else if(matrix[i][j] < target)
{
i++;
}
else
{
j--;
}
}
return false;
}
};

Leetcode240. Search a 2D Matrix II搜索二维矩阵2的更多相关文章

  1. 240 Search a 2D Matrix II 搜索二维矩阵 II

    编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性:    每行的元素从左到右升序排列.    每列的元素从上到下升序排列.例如,考虑下面的矩阵:[  [1,   4,  7 ...

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

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

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

  5. LeetCode240: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 ...

  6. Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)

    Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...

  7. LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37

    240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...

  8. LeetCode 74. 搜索二维矩阵(Search a 2D Matrix)

    74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...

  9. leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II

    74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...

随机推荐

  1. JPA 基本使用

    ORM简介 对象关系映射(Object Relational Mapping,简称ORM),是一种程序技术,用于实现面向对象编程语言里不同类型系统的数据之间的转换. 实现ORM思想的框架:Mybati ...

  2. 应用上云新模式,Aliware 全家桶亮相杭州云栖大会

    全面上云带来的变化,不仅是上云企业数量上的攀升,也是企业对云的使用方式的转变,越来越多的企业用户不仅将云作为一种弹性资源,更是开始在云上部署架构和应用,借助 Serverless 等技术,开发人员只需 ...

  3. create table常用命令

    CREATE TABLE students( stuID INTEGER NOT NULL , stuname ) not null, sex int NOT NULL ); CREATE TABLE ...

  4. P2528 [SHOI2001]排序工作量之新任务

    P2528 [SHOI2001]排序工作量之新任务 题目描述 假设我们将序列中第i件物品的参数定义为Ai,那么排序就是指将A1,…,An从小到大排序.若i<j且Ai>Aj,则<i ...

  5. 实时检测网络状态及是否可以连接Internet

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  6. k8s-prometheus监控

  7. Mybatis 动态insert动态插入的坑

    在写insert子句的时候,由于不知道需要插入多少字段,mybatis通过prefix,suffix,suffixOverrides很好的解决了该问题,实现了动态insert语句. 用这种动态插入时& ...

  8. Hadoop 2.x 版本中的依赖 jar

  9. nodejs入门最简单例子

    一.mac话,先安装nodejs环境: brew install nodejs 二.先写一个main.js var http = require("http"); http.cre ...

  10. soj97 旅行

    题意:给你一棵n个点的树.m个操作,op 1:在点i上建立银行.op 2:询问从点x开始可以经过至少一个银行走到的点中编号第二大的点. n,m<=1e5. 标程: #include<bit ...