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、空指针

  • 相关阅读:
    Balance_01背包
    4 Values whose Sum is 0_upper_bound&&ower_bound
    Newspaper Headline_set(upper_bound)
    lower_bound和upper_bound算法
    Blocks_DP&&矩阵快速幂
    All X_数的快速幂
    Training little cats_矩阵快速幂
    Raising Modulo Numbers_快速幂取模算法
    Defining Python Source Code Encodings
    Python ord(char)
  • 原文地址:https://www.cnblogs.com/lingli-meng/p/7100061.html
Copyright © 2011-2022 走看看