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. 《Javascript设计模式》笔记一js的表现力

    用不同方法完成同样一个任务:启动和停止动画. 1.过程式的程序设计: function startAnimation(){ ... } function stopAnimation(){ ... } ...

  2. marquee标签实现页面内容的滚动效果

    页面的自动滚动效果,可由javascript来实现, 但是有一个html标签 - <marquee></marquee>可以实现多种滚动效果,无需js控制. 使用marquee ...

  3. Java 和 Google Chrome 浏览器

    来源:https://java.com/zh_CN/download/faq/chrome.xml 本文适用于: 浏览器: Chrome Java 版本: 7.0, 8.0 Chrome 不再支持 N ...

  4. 多项目开发下的dll文件管理

    阅读目录: DS01:为什么要对生成的dll文件进行管理? DS02:首先介绍以下两个DOS命令 DS03:第一种实现方法(xcopy) DS04:第二种实现方法(attrib) DS05:分享一个有 ...

  5. Ubuntu下Nutch1.2的使用

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAeMAAABpCAIAAACGSdxlAAAAA3NCSVQICAjb4U/gAAAgAElEQVR4Xu ...

  6. #1000 A + B (hihoCoder)

    时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 求两个整数A+B的和 输入 输入包含多组数据.每组数据包含两个整数A(1 ≤ A ≤ 100)和B(1 ≤ A ≤ 100) ...

  7. wpf初步-grid布局-连连看棋盘

    private void Window_Loaded_1(object sender, RoutedEventArgs e) { //Button btn1 = new Button(); //btn ...

  8. C/C++程序员常去网站

    www.codeproject.comwww.codegru.comwww.chinaunix.netwww.csdn.netwww.vckbase.com http://www.google.com ...

  9. python2.7安装scikit-learn遇到的问题及解决方法

    安装完matplotlib后,在运行scikit-learn相关的库的时候又遇到缺包的问题,本来以为缺什么包就装什么包,但是由于种种原因,使我走上了弯路: 第一个坑:学校校园网限制.我用scikit- ...

  10. Leetcode_实现zigzag的转换_20161228

    #include<iostream> //#include<valarray> #include<vector> #include<string> us ...