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

  • 相关阅读:
    [转] KVM I/O slowness on RHEL 6
    QEMU KVM libvirt 手册(3)
    QEMU KVM libvirt手册(2): monitor
    QEMU KVM libvirt 手册(1): 安装
    CentOS7下JSP连接Mysql
    使用Tomcat搭建基于域名的虚拟机
    CentOS7下搭建Tomcat服务器
    Nginx在线服务状态下平滑升级或新增模块
    源码安装LNMP
    二进制包安装Mysql
  • 原文地址:https://www.cnblogs.com/lingli-meng/p/7100061.html
Copyright © 2011-2022 走看看