zoukankan      html  css  js  c++  java
  • 3,二维数组查找

     题意:每行每列递增,判断数组是否有target,数组有*重复*元素。
    思路1:二分,从最右上角开始比较,<target,该行排除,>target,该列排除。

        public class Solution {
        public boolean Find(int target, int [][] array) {
            if (array == null || array.length == 0) {
                return false;
            }
            if (array[0] == null || array[0].length == 0) {
                return false;
            }
            int rows = array.length;
            int cols = array[0].length;
            boolean found = false;
            int row = 0;
            int col = array[0].length - 1;
            while(row < rows && col >= 0){
                if (array[row][col] == target){
                    found = true;
                    break;
                } else if (array[row][col] < target){
                    row++;
                } else {
                    col--;
                }
            }
            return found;
        }
        }
    测试用例:
    1、数组有要找的元素(该元素是最大,最小、介于之间)
    2、没有要找的元素(该元素是超出最大,最小、介于之间没有)
    3、空指针

  • 相关阅读:
    sqlserver 保留2位小数的写法
    Kettle 数据预览 乱码
    finereport 数据分析预览 居中 参数分割 自动查询
    Unable to locate value meta plugin of type (id)
    mysql8.0
    MySQL 搭建MHA高可用架构
    Java性能调优工具
    helm 部署etcd
    阿里云pv 使用
    ldconfig 引起的事故
  • 原文地址:https://www.cnblogs.com/lingli-meng/p/7100061.html
Copyright © 2011-2022 走看看