https://leetcode.com/problems/search-a-2d-matrix-ii/

巧解题,矩阵本身等于了一个binary search tree,从中值开始走

时间复杂度 O(m+n)

public boolean searchMatrix(int[][] matrix, int target) {
  if(matrix.length==0 || matrix[0].length==0) return false;

  int n = matrix.length;
  int m = matrix[0].length;
  int x = n-1;
  int y = 0;
  while( x>=0 && y<m) {
    if (matrix[x][y]==target) {
      return true;
    }
    if (matrix[x][y]< target) {
      y++;
    }
    else {
      x--;
    }
  }
  return false;
}

amazon oa1 - search in 2D array II [Leetcode] 240的更多相关文章

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

    Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...

  2. LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37

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

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

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

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

  5. LeetCode -- Search a 2D Matrix & Search a 2D Matrix II

    Question: Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matr ...

  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. LintCode 38. Search a 2D Matrix II

    Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of ...

  8. 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, ret ...

  9. [LeetCode] 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 ...

随机推荐

  1. Mac +WebStorm+nodeJs+Freemarker.js的安装与使用

    第一步用webStorm新建node+express项目 第二步执行npm i –save freemarker.js,安装 freemarker.js 模块 第三步安装java jdk包 jdk包地 ...

  2. guava学习--Preconditions

    转载:https://my.oschina.net/realfighter/blog/349819 Preconditions是guava提供的用于进行代码校验的工具类,其中提供了许多重要的静态校验方 ...

  3. centOS 6.7 中安装matlab R2014b

    参考资料: [1] http://www.centoscn.com/image-text/config/2014/1222/4354.html 系统: centOS 6.7 2.6.32-573.el ...

  4. Leetcode 详解(Substing without repeats character)

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  5. [移动端]rem适配

    原理:给html根节点设置一个基础font-size值,然后页面的所有元素布局均相对于该font-size值采用rem单位设定.font-size的取值通过js计算. 但字体不用rem单位,原因如下: ...

  6. 1171. Lost in Space

    http://acm.timus.ru/problem.aspx?space=1&num=1171 一天的时间,WA了N遍,居然是因为数组开小了呀,我勒个去!鄙视自己...... 我是从第 1 ...

  7. git检出与创建的过程

    Command line instructions Git global setup git config --global user.name "bingo" git confi ...

  8. CentOS 6.6编译安装Nginx1.6.2+MySQL5.6.21+PHP5.6.3

    http://www.osyunwei.com/archives/8867.html 一.配置防火墙,开启80端口.3306端口 vi /etc/sysconfig/iptables #编辑防火墙配置 ...

  9. android studio集成融云 SDK 后在部分机型启动对话时崩溃

    最初构建项目是 eclipse, 后来切换到 android studio来做开发. 后来多个用户反馈在android4.4机型上存在启动对话崩溃的问题.但是IOS 版工程从来没有重现. 调试报错信息 ...

  10. 测试数学公式latex

    \( J_\alpha(x) = \sum\limits_{m=0}^\infty \frac{(-1)^m}{m! + 1)}{\left({\frac{x}{2}}\right)}^{2 m + ...