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 from left to right.
  • The first integer of each row is greater than the last integer of the previous row.

For example,

Consider the following matrix:

[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]

Given target = 3, return true.

思路:此题还是比較简单的。主要是用两个二分法,才对第一列使用,再对特定的行使用。

详细代码例如以下:

public class Solution {
public boolean searchMatrix(int[][] m, int target) {
//两个二分搜索
//先搜索第一列,找到确定的行,然后再搜索行
int i = 0;
int j = m.length-1;
int mid = 0;
//搜寻第一列
while(i <= j){
mid = (i + j)/2;
if(m[mid][0] == target){
return true;
}else if(m[mid][0] < target){
i = mid + 1;
}else{
j = mid - 1;
}
}
if(m[mid][0] > target){
if(mid == 0){
return false;
}
mid--;//mid-1
}
//搜寻mid行
i = 0;
j = m[0].length -1;
int k = mid;
//搜寻到了返回true
while(i <= j){
mid = (i + j)/2;
if(m[k][mid] == target){
return true;
}else if(m[k][mid] < target){
i = mid + 1;
}else{
j = mid - 1;
}
}
//没有搜寻到,返回false
return false;
}
}

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

    编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性:    每行中的整数从左到右排序.    每行的第一个整数大于前一行的最后一个整数.例如,以下矩阵:[  [1,   3, ...

  3. Leetcode74. Search a 2D Matrix搜索二维矩阵

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

  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 74. Search a 2D Matrix 、240. Search a 2D Matrix II

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

  6. [LeetCode] 74 Search a 2D Matrix(二分查找)

    二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...

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

  9. search a 2D matrix(在二维数组中搜索一个元素)

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

随机推荐

  1. IO流遍历文件夹下所有文件问题

    import java.io.File; /** * @author 王恒 * @datetime 2017年4月20日 下午2:24:32 * @description 递归调用 * */ publ ...

  2. CSS多列布局(实例)

    前言 一列布局 二列布局 三列布局 1 一列布局 一列布局: HTML部分 <!DOCTYPE html> <html> <head> <meta chars ...

  3. 用DIV遮罩解决checkbox勾选无效的问题

    在前端开发的过程中,遇到一种情况,需要勾选,为了用户的操作便捷就将click事件放到了DIV上.(其中使用了knockout.js) 代码大概如下: <div id="one" ...

  4. C#中SetWindowPos函数详解

    [DllImport("user32.dll")] private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWnd ...

  5. 《Java编程思想》学习笔记(一)

    1——面向对象和JVM基础  1.java中的4种访问制权限: (1).public:最大访问控制权限,对所有的类都可见. (2).protect:同一包可见,不在同一个包的所有子类也可见. (3). ...

  6. C# 聚合数据借口发短信的使用

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

  7. antd #upload

    import React from 'react' import {Upload, Icon, message,Button } from 'antd' import './index.scss'; ...

  8. Arrays工具类的使用

    1.包: java.util 导包 2.此类包含用来操作数组(比如排序和搜索)的各种方法 特点: 该类中的方法都是静态方法,所以可以直接使用类名.方法名(实参)调用 3.查看成员方法: public ...

  9. wget 批量下载网站目录下的文件

    执行如下命令就会自动下载 http://www.iyunwei.com/docs/ 下面的所有文件: wget -nd -r -l1 --no-parent http://www.iyunwei.co ...

  10. BZOJ 1579: [Usaco2009 Feb]Revamping Trails 道路升级 分层图最短路 + Dijkstra

    Description 每天,农夫John需要经过一些道路去检查牛棚N里面的牛. 农场上有M(1<=M<=50,000)条双向泥土道路,编号为1..M. 道路i连接牛棚P1_i和P2_i ...