找出数组中重复的数字。
    在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。
    示例 1:
    输入:

    [2, 3, 1, 0, 2, 5, 3] 输出:2 或 3

    限制:
    2 <= n <= 100000

    1. class Solution {
    2. public int findRepeatNumber(int[] nums) {
    3. if(nums == null|| nums.length == 0) return -1;
    4. int res = -1;
    5. Set<Integer> set = new HashSet();
    6. for(int i = 0; i < nums.length; i++) {
    7. //如果添加的元素已经存在,那么就会返回false
    8. if(!set.add(nums[i])){
    9. res = nums[i];
    10. break;
    11. }
    12. }
    13. return res;
    14. }
    15. }
    1. class Solution {
    2. public void swap(int[] nums,int i, int j) {
    3. int temp = nums[i];
    4. nums[i] = nums[j];
    5. nums[j] = temp;
    6. }
    7. public int findRepeatNumber(int[] nums) {
    8. if( nums == null || nums.length == 0) return -1;
    9. for(int i= 0; i < nums.length; i++) {
    10. while(nums[i] != i && nums[nums[i]] != nums[i]) {
    11. swap(nums,i,nums[i]);
    12. }
    13. if(nums[i] == i){
    14. return nums[i];
    15. }
    16. }
    17. return -1;
    18. }
    19. }