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. python读取excel的行数

    基于python3.x下 需要包 from openpyxl import load_workbook 代码如下: from openpyxl import load_workbook wb = lo ...

  2. Intellij IDEA 的使用(创建项目、导入项目、同时部署多个项目、JRebel)等常见eclipse、myeclipse换idea必看

    第一篇:Intellij IDEA 的使用 1.黑色主题 中文乱码修改 2.WEB项目的部署 以及自动编译 3.多项目的同时部署 4.相关插件提高工作效率 1.JRebel插件 实现热部署 2.Tas ...

  3. Oracle 支持正则表达式的函数

    内容提要 oracle 10g 增加的正则表达式函数有以下四种: regexp_like() --返回满足条件的字段 regexp_instr() --返回满足条件的字符或字符串的位置 regexp_ ...

  4. Google Play开发者账号注册与失败申诉攻略

    Google Play开发者账号注册与失败申诉攻略 这篇文章我在网上找了好久,是在Google play进行开发者账号注册方法,介绍的很详细.现在分享一下.[原文地址] 为了方便开发者们注册谷歌的官方 ...

  5. Angular学习(7)- 模板

    示例: <!DOCTYPE html> <html ng-app="MyApp"> <head> <title>Study 7< ...

  6. UIview定义背景图片

    UIImage *image = [UIImage imageNamed:@"bgimagename"];    UIView *view = [[UIView alloc]ini ...

  7. SPOJ #500. Turbo Sort

    Sorting is not an out-dated topic. My own in-place qsort got TLE... so, I simply called stl::sort() ...

  8. lucene之排序、设置权重、优化、分布式搜索(转)

    lucene之排序.设置权重.优化.分布式搜索(转) 1. 基本应用 using System;using System.Collections.Generic;using System.Text;u ...

  9. SSH_框架整合6--修改Edit员工信息

    SSH_框架整合6--修改Edit员工信息 1 加上修改Edit键 (1)emp-list.jsp <td> <a href="emp-input?id=${id }&qu ...

  10. bzoj1006 神奇的国度

    Description K国是一个热衷三角形的国度,连人的交往也只喜欢三角原则.他们认为三角关系:即AB相互认识,BC相互认识,CA相互认识,是简洁高效的.为了巩固三角关系,K国禁止四边关系,五边关系 ...