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. [
  2. [1, 3, 5, 7],
  3. [10, 11, 16, 20],
  4. [23, 30, 34, 50]
  5. ]

Given target = 3, return true.

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

详细代码例如以下:

  1. public class Solution {
  2. public boolean searchMatrix(int[][] m, int target) {
  3. //两个二分搜索
  4. //先搜索第一列,找到确定的行,然后再搜索行
  5. int i = 0;
  6. int j = m.length-1;
  7. int mid = 0;
  8. //搜寻第一列
  9. while(i <= j){
  10. mid = (i + j)/2;
  11. if(m[mid][0] == target){
  12. return true;
  13. }else if(m[mid][0] < target){
  14. i = mid + 1;
  15. }else{
  16. j = mid - 1;
  17. }
  18. }
  19. if(m[mid][0] > target){
  20. if(mid == 0){
  21. return false;
  22. }
  23. mid--;//mid-1
  24. }
  25. //搜寻mid行
  26. i = 0;
  27. j = m[0].length -1;
  28. int k = mid;
  29. //搜寻到了返回true
  30. while(i <= j){
  31. mid = (i + j)/2;
  32. if(m[k][mid] == target){
  33. return true;
  34. }else if(m[k][mid] < target){
  35. i = mid + 1;
  36. }else{
  37. j = mid - 1;
  38. }
  39. }
  40. //没有搜寻到,返回false
  41. return false;
  42. }
  43. }

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. 分别使用Hadoop和Spark实现TopN(1)——唯一键

    0.简介 TopN算法是一个经典的算法,由于每个map都只是实现了本地的TopN算法,而假设map有M个,在归约的阶段只有M x N个,这个结果是可以接受的并不会造成性能瓶颈. 这个TopN算法在ma ...

  2. js数据管理的思考

    最近要做一个农场项目,涉及到很多js数据管理的需求,这里也做下总结,不断的总结,再修正内容,也是快速进步的方法. 数据管理几个方面考虑: * 设置(更新)字段值 * 检索,根据id, index, 属 ...

  3. 最简单的多线程死锁案例代码(Java语言)

    package com.thread.test; public class DeadLock { private static Object firstMonitor = new Object(); ...

  4. classNum 表示学生的班号,例如“class05”。 有如下List  List list = new ArrayList();

    package a927; import java.util.ArrayList; import java.util.List; class Student { private String name ...

  5. Linux学习自动化脚本(一)

    https://www.cnblogs.com/handsomecui/p/5869361.html https://blog.csdn.net/daigualu/article/details/76 ...

  6. ROS:Nvidia Jetson TK1平台安装使用ROS

    原文连接: http://wiki.ros.org/indigo/Installation/UbuntuARM Ubuntu ARM install of ROS Indigo There are c ...

  7. 通用功能类:改变WinForm窗体显示颜色

    一.显示窗体调用方法 protected override void OnLoad(EventArgs e)        {            MDIClientSupport.SetBevel ...

  8. sql 排序

    select count(*) from vote group by contents PERCENT * from vote order by contents)as A group by cont ...

  9. scrapy获取重定向之前的url

    通过 response.request.meta['redirect_urls'] 来获取跳转之前的链接

  10. esp32(M5STACK)在线体验(Ubuntu)

    我们往m5stack烧录的固件是可以在线编程的 具体使用方法可以参考   https://github.com/m5stack/M5Cloud/blob/master/README_CN.md     ...