1.题目 找出数组中重复的数字。 在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。 示例 1: 输入: [2, 3, 1, 0, 2, 5, 3] 输出:2 或 3 限制: 2 <= n <= 100000 2.思考 给定长度n的数组,并且数字为0 - n-1,我可以创建一个长度为n的数组用来计数,数组下标对应给定数组的数字遍历数组计数遍历新数组,遇到数组内容大于1,则输出此位置 3.我的代码代码 class Solution { public int findRepeatNumber(int[] nums) { int[] count = new int[nums.length]; for (int i = 0; i < nums.length; i++) { count[nums[i]]++; } for (int i = 0; i < count.length; i++) { if (count[i] > 1) return i; } return 0; } 4.题解代码 class Solution { public int findRepeatNumber(int[] nums) { Set<Integer> set = new HashSet<>(); int repeat = -1; for (int num: nums) { if (!set.add(num)) { repeat = num; break; } } return repeat; } }