• JZ050数组中重复的数字


    数组中重复的数字

    题目描述

    在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中

    • 第一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。
    • 返回描述:
    • 如果数组中有重复的数字,函数返回true,否则返回false。
    • 如果数组中有重复的数字,把重复的数字放到参数duplication[0]中。(ps:duplication已经初始化,可以直接赋值使用。)

    题目链接: 数组中重复的数字

    代码

    /**
     * 标题:数组中重复的数字
     * 题目描述
     * 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中
     * 第一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。
     * 返回描述:
     * 如果数组中有重复的数字,函数返回true,否则返回false。
     * 如果数组中有重复的数字,把重复的数字放到参数duplication[0]中。(ps:duplication已经初始化,可以直接赋值使用。)
     * <p>
     * 题目链接
     * https://www.nowcoder.com/practice/623a5ac0ea5b4e5f95552655361ae0a8?tpId=13&&tqId=11203&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
     */
    public class Jz50 {
    
        /**
         * 暴力破解
         *
         * @param numbers
         * @param length
         * @param duplication
         * @return
         */
        public static boolean duplicate(int[] numbers, int length, int[] duplication) {
            if (length <= 1) {
                return false;
            }
    
            for (int i = 0; i < length - 1; i++) {
                for (int j = i + 1; j < length; j++) {
                    if (numbers[j] == numbers[i]) {
                        duplication[0] = numbers[i];
                        return true;
                    }
                }
            }
            return false;
        }
    
        public static boolean duplicate1(int[] nums, int length, int[] duplication) {
            if (nums == null || length <= 0) {
                return false;
            }
            for (int i = 0; i < length; i++) {
                while (nums[i] != i) {
                    if (nums[i] == nums[nums[i]]) {
                        duplication[0] = nums[i];
                        return true;
                    }
                    swap(nums, i, nums[i]);
                }
            }
            return false;
        }
    
        public static void swap(int[] nums, int i, int j) {
            int t = nums[i];
            nums[i] = nums[j];
            nums[j] = t;
        }
    
        public static void main(String[] args) {
            int[] numbers = {2, 3, 1, 0, 2, 5, 2};
            int[] duplication = new int[1];
            int[] duplication2 = new int[1];
            int[] duplication11 = new int[1];
            boolean duplicate = duplicate(numbers, 7, duplication);
            System.out.println(duplicate);
            System.out.println(duplication[0]);
    
            boolean duplicate1 = duplicate1(numbers, 7, duplication2);
            System.out.println(duplicate1);
            System.out.println(duplication2[0]);
        }
    }
    

    【每日寄语】 当你不开心的时候,你就可以吃一块糖果,然后告诉自己生活还是甜甜的,加油。

  • 相关阅读:
    牛客小白月赛12 D 月月给华华出题 (欧拉函数,数论,线筛)
    牛客小白月赛12 F 华华开始学信息学 (分块+树状数组)
    牛客小白月赛12 C 华华给月月出题 (积性函数,线性筛)
    牛客小白月赛12 I 华华和月月逛公园 (tarjian 求桥)
    Tourist's Notes CodeForces
    Educational Codeforces Round 71 (Rated for Div. 2) E XOR Guessing (二进制分组,交互)
    Tunnel Warfare HDU
    蓝桥杯第三届总决赛
    HDU 1695(数论,筛选+素因子分解+容斥)
    lightoj 1248 Dice (III)(几何分布+期望)
  • 原文地址:https://www.cnblogs.com/kaesar/p/15777489.html
Copyright © 2020-2023  润新知