剑指Offer_03_数组中重复的数字

package 二刷;

import java.util.HashSet;
import java.util.Set;

/**
 * https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof/
 *
 * @author Qitong!!
 * @Date 2020/6/30
 */
public class 剑指Offer_03_数组中重复的数字 {
    //利用 HashSet  时间O(N) 空间O(N)
    public int findRepeatNumber(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            if (set.contains(num)) {
                return num;
            }
            set.add(num);
        }
        return -1;
    }

    //遍历一遍交换位置  时间O(N) 空间O(1)
    public int findRepeatNumber2(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            while (nums[i] > i) {
                if (nums[i] == nums[nums[i]]) return nums[i];
                swap(nums, nums[i], i);
            }
            if (nums[i] == i) continue;
            return nums[i];
        }
        return -1;
    }

    private void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_45399846/article/details/107449161