zoukankan      html  css  js  c++  java
  • 二维数组中的查找

    二维数组中的查找

    在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

    题解:做矩阵左下角或是右上角开始查询,当target存在的时候,当nums[i][j]<target的时候,j++;当nums[i][j]>target的时候,i--;注意i, j别越界。

    完整代码

     1 /**
     2  * @author: wooch
     3  * @create: 2020/02/14
     4  */
     5 
     6 /**
     7  * 面试题04. 二维数组中的查找
     8  * 在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。
     9  * 请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
    10  */
    11 public class P04_FindNumberIn2DArray {
    12     public boolean findNumberIn2DArray(int[][] matrix, int target) {
    13         if(matrix.length == 0){
    14             return false;
    15         }
    16         int n = matrix.length;
    17         int m = matrix[0].length;
    18         int i = 0;
    19         int j = m - 1;
    20         while (i < n && j >= 0) {
    21             if (target < matrix[i][j]) {
    22                 j--;
    23             } else if (target > matrix[i][j]) {
    24                 i++;
    25             } else {
    26                 return true;
    27             }
    28         }
    29         return false;
    30     }
    31 }
  • 相关阅读:
    unity, texture import settings
    unity, 最简单的additive shader
    unity, shader input and output
    unity, multi pass shader中的surface pass
    unity, 荧光效果(bloom)
    unity, 查看内置shader源码
    unity, Find References In Scene
    unity 主循环
    unity 显示帧率
    HttpUrlConnection的setDoOutput与setDoInput的区别
  • 原文地址:https://www.cnblogs.com/baishouzu/p/12308294.html
Copyright © 2011-2022 走看看