zoukankan      html  css  js  c++  java
  • (数组)数组中重复的数字 ,二维数组中的查找

    链接:https://www.nowcoder.com/questionTerminal/623a5ac0ea5b4e5f95552655361ae0a8
    来源:牛客网

    在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。

    解法一:将输入的数组从小到大排序,再从头到尾扫描,找到重复的元素。

    O(nlogn)

    class Solution {
    public:
        // Parameters:
        //        numbers:     an array of integers
        //        length:      the length of array numbers
        //        duplication: (Output) the duplicated number in the array number
        // Return value:       true if the input is valid, and there are some duplications in the array number
        //                     otherwise false
        bool duplicate(int numbers[], int length, int* duplication) {
            sort(numbers, numbers+length);
            for(int i=1; i<length;i++){
                if(numbers[i] == numbers[i-1]){
                    *duplication = numbers[i-1];
                    return true;
                }
            }
            return false;
        }
    };

    解法二:

    时间复杂度:O(n)

    从头到尾扫描数组,若第i个位置上的元素不等于numbers[i],若numbers[numbers[i]] != numbers[i], 则交换 numbers[numbers[i]] 和 numbers[i] ,直到使得第i个位置上的元素等于numbers[i] 为止。

    若numbers[numbers[i]] == numbers[i],则找到了一个重复的元素,返回true。

    class Solution {
    public:
        // Parameters:
        //        numbers:     an array of integers
        //        length:      the length of array numbers
        //        duplication: (Output) the duplicated number in the array number
        // Return value:       true if the input is valid, and there are some duplications in the array number
        //                     otherwise false
        bool duplicate(int numbers[], int length, int* duplication) {
            if(numbers == NULL || length<=0)
                return false;
            for(int i=0; i<length; i++){
                while(numbers[i]!=i){
                    if(numbers[i] == numbers[numbers[i]]){
                        *duplication = numbers[i];
                        return true;
                    }
                    else{
                        swap(numbers[i], numbers[numbers[i]]);
                    }
                }
                return false;
            }
        }  
    };

    二维数组中的查找 

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

    https://www.nowcoder.com/questionTerminal/abc3fe2ce8e146608e868a70efebf62e

    从二维矩阵的右上角的数字开始与target比较,若该数字等于target,则return true;若大于target,则剔除这个数字所在列;若小于target,则剔除这个数字所在行。

    class Solution {
    public:
        bool Find(int target, vector<vector<int> > array) {
            if(array.empty() || array[0].empty()) return false;
            int n = array.size();
            int m = array[0].size();
            int i = 0, j = m-1;
            while(i<n && j>=0){
                if(target == array[i][j]) return true;
                else if(target < array[i][j])
                    j--;
                else
                    i++;
            }
            return false;
        }
    };
  • 相关阅读:
    ORACLE的程序包1程序包的基
    JAVA中方法重载,方法覆盖,方法继承等小结
    使用DBMS_JOB包创建ORACLE定时任务
    linux shell 中判断语句
    oracle direction目录
    Java加载类的加载顺序
    Struts2非常简单实用的身份验证功能
    关于ListView优化的一点心得
    使用webview将网页打包成apk
    关于android下的冒烟测试
  • 原文地址:https://www.cnblogs.com/Bella2017/p/11819477.html
Copyright © 2011-2022 走看看