zoukankan      html  css  js  c++  java
  • 剑指01二维数组的查找

    题目描述

    在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
    class Solution {
    public:
        bool Find(int target, vector<vector<int> > array) {
            //array 是二维数组,这里没做判空操作
            int rows=array.size();
            int cols=array[0].size();
            int i=rows-1,j=0;//左下角元素坐标
            while (i>=0 && j<cols){
                //使其不超出数组范围
                if (target<array[i][j])
                    i--;//查找的元素较少,往上找
                else if (target >array[i][j])
                    j++;//查找的元素较大,往右找
                else
                    return true;//找到
            }
            return false;
            
        }
    };
    public class Solution {
        public boolean Find(int target, int [][] array) {
            int rows=array.length;
            int cols=array[0].length;
            int i=rows-1,j=0;
            while (i>=0 && j<cols){
                if (target <array[i][j])
                    i--;
                else if (target>array[i][j])
                    j++;
                else
                    return true;
            }
            return false;
        }
    }
    # -*- coding:utf-8 -*-
    class Solution:
        # array 二维列表
        def Find(self, target, array):
            # write code here
            rows=len(array)-1
            cols=len(array[0])-1
            i=rows
            j=0
            while j<=cols and i>=0:
                if target <array[i][j]:
                    i-=1
                elif target>array[i][j]:
                    j+=1
                else :
                    return True
            return False
       
  • 相关阅读:
    屏蔽/捕获并输出错误
    物理机转Hyper-V虚拟机
    Windows Server 2012无法安装 .NET3.5-安装角色或功能失败,找不到源文件
    IDRAC 固件升级操:
    网卡启动安装dell服务器OS
    服务器指定网卡进行备份数据避免影响业务口
    【转载】用户通过WEB方式更改AD域帐户密码
    Windows运维之Windows8.1-KB2999226-x64安装提示 此更新不适用你的计算机
    Exchange 退信550 5.1.11 RESOLVER.ADR.ExRecipNotFound
    优酷kux视频转MP4
  • 原文地址:https://www.cnblogs.com/hrnn/p/13401990.html
Copyright © 2011-2022 走看看