编写一个高效的算法来搜索 m x n 矩阵中的一个目标值。该矩阵具有以下特性:
    每行中的整数从左到右排序。
    每行的第一个整数大于前一行的最后一个整数。
例如,
以下矩阵:
[
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
]
给定 目标值= 3,返回 true。
详见:https://leetcode.com/problems/search-a-2d-matrix/description/

Java实现:

class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int row=matrix.length-1;
int col=0;
while(row>=0&&col<matrix[0].length){
if(matrix[row][col]==target){
return true;
}else if(matrix[row][col]>target){
--row;
}else{
++col;
}
}
return false;
}
}

C++实现:

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

参考:https://blog.csdn.net/zhangxiao93/article/details/49835511

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

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

  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. Leetcode74. Search a 2D Matrix搜索二维矩阵

    编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 1: 输入: matrix ...

  4. 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 OJ:Search a 2D Matrix(二维数组查找)

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

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

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

    Leetcode之二分法专题-240. 搜索二维矩阵 II(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 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37

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

随机推荐

  1. 分布式锁的实现方式——ACID数据库、缓存或者是zk

    针对分布式锁的实现,目前比较常用的有以下几种方案: 基于数据库实现分布式锁 基于缓存(redis,memcached,tair)实现分布式锁 基于Zookeeper实现分布式锁 在分析这几种实现方案之 ...

  2. 关于JDK安装javac失效的几个问题。

    1.按照指南一步一步配置环境变量. 打开cmd,测试. 2.如果还是没有用,注意你的JAVA_HOME配置的是用户变量还是系统变量,改成系统变量. 打开cmd,测试. 3.如果还是没有用,不要你的JA ...

  3. 机器视觉 Local Binary Pattern (LBP)

    Local binary pattern (LBP),在机器视觉领域,是非常重要的一种特征.LBP可以有效地处理光照变化,在纹理分析,纹理识别方面被广泛应用. LBP 的算法非常简单,简单来说,就是对 ...

  4. js将时间转换为时间戳

    转自http://zhidao.baidu.com/link?url=jwmRLUKIC92fNeS1l8PuZltmZIN--LJFtKd9G6SYEjFfCu_pFGyXsh54txzv22E0g ...

  5. 图形化升级单机oracle 11.2.0.1 到 11.2.0.4

    1. 讲补丁包上传到 oracle server ,解压.安装 [root@11g ~]#unzip p13390677_112040_Linux-x86-64_1of7.zip -d /tmp &a ...

  6. jquery中attr() & prop() 的区别与其实现方法

    $(function(){ $('#check').attr('checked'); // undefind ???一头雾水 }) 在jquery中 attr 本来就是用来设置或者获取属性的,可是上面 ...

  7. 如何更改linux文件的拥有者及用户…

    本文整理自: http://blog.163.com/yanenshun@126/blog/static/128388169201203011157308/ http://ydlmlh.iteye.c ...

  8. Struts2返回JSON数据的具体应用范…

    Struts2返回JSON数据的具体应用范例 博客分类: Struts2 Struts2JSON  早在我刚学Struts2之初的时候,就想写一篇文章来阐述Struts2如何返回JSON数据的原理和具 ...

  9. sqlserver2012——变量declare

    1.声明变量病定义类型 赋值操作 ) set @name='小明' select @name 使用select进行赋值 ) select @name='李明' seelelct @name

  10. MS SQL的CASE...WHEN...THEN...END语法

    根据多个可能的答案检查一个值或变量. 举例说明: SELECT [type],CASE [type] WHEN 'TT' THEN 'TYPE_TABLE' WHEN 'FN' THEN 'SQL_S ...