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.

解法一:暴力解法,遍历二维数组。

代码如下:

public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int rows=matrix.length;
int columns=matrix[0].length;
for(int i=0;i<rows;i++)
for(int j=0;j<columns;j++){
if(target==matrix[i][j])
return true;
}
return false;
}
}

  运行结果:时间复杂度为O(M*N)

方法二:根据矩阵的特点,从右上角元素开始比较,target小,则列数减1,target大,则行数加1.

代码如下:

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

  运行结果:时间复杂度O(M+N)

(medium)LeetCode 240.Search a 2D Matrix II的更多相关文章

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

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

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

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

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

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

  5. 【LeetCode】240. Search a 2D Matrix II

    Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...

  6. 【刷题-LeetCode】240. Search a 2D Matrix II

    Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...

  7. 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【leetcode】74. Search a 2D Matrix & 240. Search a 2D Matrix II

    题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是 ...

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

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

随机推荐

  1. Spring 注解总结

    声明:这是转载的.内容根据网上资料整理.相关链接:http://www.360doc.com/content/10/1118/16/2371584_70449913.shtmlhttp://www.i ...

  2. UI-程序的运行顺序

    在AppDelegate.m里面的每个方法里都输入(包括main文件里也输入) NSLog(@"%s %d”,__func__,__LINE__); __func__ :代表使用的方法 __ ...

  3. jsoncpp用法通俗易懂之解析

    刚工作不久,最近遇到一个要解析一个web服务器发过来的json格式的文件,文件如下: { "global": { "renew": "true&quo ...

  4. 剑指offer系列42---二叉树深度

    [题目]输入一棵二叉树,求该树的深度. * 从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. package com.exe9.offer; /** * [题 ...

  5. Redis-sentinel监控

    Sentinel介绍 Redis的 Sentinel 系统用于管理多个Redis服务器, 该系统执行以下三个任务: 监控(Monitoring) 提醒(Notification) 自动故障迁移(Aut ...

  6. android的Looper例子

    直接贴代码 MsgThread.java package bb.aa.looperdemo; import android.os.Handler; import android.os.Looper; ...

  7. 黄聪:Mysql5.6缓存命中率

    MySQL缓存命中率,网上说法不一,下面我说下我的看法,大家轻拍: 总的select查询数等于com_select(没命中) + qcache_hits(命中) + 解析错误的查询. 再来看看Com_ ...

  8. Java执行main方法,异常为:could not find the main class.program will exit

    未解决. Java执行方法,异常为:could not find the main class.program will exitmain 原文地址:http://rogerfederer.iteye ...

  9. NeHe OpenGL教程 第三十课:碰撞检测

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  10. linux 命令(1)screen

    一.screen的安装和用法 Screen是一个可以在多个进程之间多路复用一个物理终端的窗口管理器,这意味着你能够使用一个单一的终端窗口运行多终端的应用. Screen中有会话的概念,用户可以在一个s ...