LeetCode287_287. Finding Duplicates

LeetCode287_287. Finding Duplicates

1. Description

Given an array nums containing n + 1 integers, whose numbers are all in the range [1, n] (including 1 and n), we know that there is at least one duplicate integer.

Assuming nums has only one repeating integer, return the repeating number.

The solution you design must not modify the array nums and use only constant O(1) extra space.

Example 1:

输入:nums = [1,3,4,2,2]
输出:2

Example 2:

输入:nums = [3,1,3,4,2]
输出:3

hint:

1 <= n <= 10 to the 5th power
nums.length == n + 1
1 <= nums[i] <= n
In nums, only one integer appears twice or more, and the rest of the integers appear only once

Two, solution

Method one: HashSet. It's a pity that the title requires that only constant O(1) extra space can be used.

    //方法一:HashSet。很可惜题目要求,只能用常量级 O(1) 的额外空间。
    //所以这个在本地测试测试就行了。当然提交也可以AC通过
    /*
    执行结果:通过
    执行用时:33 ms, 在所有 Java 提交中击败了11.91%的用户
    内存消耗:57 MB, 在所有 Java 提交中击败了98.35%的用户
    通过测试用例:58 / 58
     */
    public int findDuplicate(int[] nums) {
    
    
        //因为题目的提示条件里面写了数组长度大于等于2了。所以关于数组的 空 或者 长度为0 判断可以在这里省略
        int res = 0;//定义一个标识,用来作为重复元素的识别。
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
    
    
            if (!set.contains(nums[i])) {
    
    
                set.add(nums[i]);
            } else {
    
    
                res = nums[i];
            }
        }
        return res;
    }

Method 2: Double-layer for. This time complexity is n squared, which is a bit high and not recommended.

    //方法二:双层for。这个时间复杂度n平方,有点高,不建议。
    //提交的时候会 超时
    public int findDuplicate2(int[] nums) {
    
    
        for (int i = 0; i < nums.length; i++) {
    
    
            for (int j = i + 1; j < nums.length; j++) {
    
    
                if (nums[i] == nums[j]) {
    
    
                    return nums[i];
                }
            }
        }
        return 0;
    }

Method 3: binary search

    //方法三:二分查找
    //AC 通过
    /*
    执行结果:通过
    执行用时:30 ms, 在所有 Java 提交中击败了13.40%的用户
    内存消耗:58.2 MB, 在所有 Java 提交中击败了92.71%的用户
    通过测试用例:58 / 58
     */
    public int findDuplicate3(int[] nums) {
    
    
        int length = nums.length; //题目要求:n + 1 = length 。

        //在 1 到 n范围内搜索
        int left = 1;
        int right = length - 1;
        while (left < right) {
    
    
            int mid = (left + right) / 2;

            int count = 0;//nums当中小于等于mid的个数
            for (int num : nums) {
    
    
                if (num <= mid) {
    
    
                    count++;
                }
            }

            if (count > mid) {
    
    //在左边
                right = mid;
            } else {
    
    //在右边
                left = mid + 1;
            }
        }
        return left;
    }

LeetCode 217. Existence of Duplicate Elements
LeetCode 229. Majority II
LeetCode 231. Powers of 2
LeetCode 234. Palindrome Linked
List LeetCode 237. Delete Nodes in a Linked List
LeetCode 242. Valid Alphabetic Words
LeetCode 257. All Paths in a Binary Tree
LeetCode 258 . Add Bits
LeetCode 263. Ugly Numbers
LeetCode 268. Missing Numbers
LeetCode 283. Moving Zeros
LeetCode 287. Finding Duplicates



Disclaimer:
        The copyright of the topic belongs to the original author. The code and related statements in the article are written by myself based on my understanding. The relevant pictures in the article are screenshots from my own practice and pictures corresponding to related technologies. If you have any objections, please contact to delete them. grateful. Reprint please indicate the source, thank you.


By luoyepiaoxue2014

Station B: https://space.bilibili.com/1523287361 Click to open the link
Weibo: http://weibo.com/luoyepiaoxue2014 Click to open the link

Guess you like

Origin blog.csdn.net/luoyepiaoxue2014/article/details/129742152