剑指刷题 查找数组中重复的数字

题目描述

在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。

解题思路:

第一种方法 就是暴力遍历  

第二种方法是 首先判断 arr[0]==0如果不是 让0 下标和arr[0]下标的元素互换位置 ,直到 arr[0]=0为止,

例子:

{2,3,1,0,2,5,3}    将下标为0 和 下标为2 互换位置

{1,3,2,0,2,5,3}   继续将下标为0 和下标为1 互换位置

{3,1,2,0,2,5,3} 继续将下标为0 和下标为3 互换位置

{0,1,2,3,2,5,3}  好了

下标i=1,i=2,i=3 的元素都与下标相等,当下标等于4的时候  arr[4] = =arr[arr[4]] ,找到了重复的元素。

class Solution {
public:
    // Parameters:
    //        numbers:     an array of integers
    //        length:      the length of array numbers
    //        duplication: (Output) the duplicated number in the array number 返回重复的数字
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    bool duplicate(int numbers[], int length, int* duplication) {
        for(int i=0;i<length;++i)
        {
            while(numbers[i]!=i)
            {
                if(numbers[i]==numbers[numbers[i]])
                {
                    *duplication=numbers[i];
                    return true;
                }
                int temp=numbers[i];
                numbers[i]=numbers[temp];
                numbers[temp]=temp;
            }
        }
        return false;
        /*//暴力遍历
        for(int i=0;i<length;++i)
        {
            for(int j=i+1;j<length;++j)
            {
                if(numbers[i]==numbers[j])
                {
                    *duplication=numbers3[i];
                    return true;
                }
            }
        
        return false;
        }*/
    }
};

go版本:

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

//暴力遍历  o(n^2)
func FindRepeatNumberByFull(nums []int) int {
	res := 0
	for index, n := range nums {
		for _, m := range nums[index+1:] {
			if n == m {
				res = n
				break
			}
		}
	}
	return res
}

//排序后比较当前数跟之前的是否不等,如果相等则重复
// O(nlogn)
func FindRepeatNumberBySort(nums []int) int {
	var res int
	sort.Ints(nums)
	length := len(nums)
	for index := 1; index < length; index++ {
		if nums[index] == nums[index-1] {
			res = nums[index]
			break
		}
	}
	return res
}

//利用哈希表,用空间换时间  最坏的情况是O(n)
func FindRepeatNumberByHash(nums []int) int {
	var res int
	arrayMap := hashmap.New()
	for _, num := range nums {
		_, exist := arrayMap.Get(num)
		if exist {
			res = num
			break
		} else {
			arrayMap.Put(num, true)
		}
	}
	return res
}

func swap(nums *[]int, index1 int, index2 int) {
	temp := (*nums)[index1]
	(*nums)[index1] = (*nums)[index2]
	(*nums)[index2] = temp
}

//空间复杂度O(1)  通过交换数组中的元素,0——n-1的数组,如果没有重复的,那么每个数字放到对应的下标即可,改下标已经有值了,那就是重复
func FindRepeatNumberBySwap(nums []int) int { //2,3,5,4,3,2,6,7
	var res int
	length := len(nums)
	for index := 0; index < length; {
		if index == nums[index] {
			index++
			continue
		}
		if nums[index] == nums[nums[index]] { // 如果和要交换的相等,就直接跳出,避免死循环交换
			res = nums[index]
			break
		}
		if index < nums[index] { //和后面元素交换
			swap(&nums, index, nums[index])
		} else { //签名的元素都是排好序的
			res = nums[index]
			break
		}
		//交换完之后 仍需比较当前下标与值是否相等
	}
	return res
}

//如果不允许改变数组的值,可以用以上思想+辅助数组
func FindRepeatNumberByArray(nums []int) int {
	var copyNums []int
	var res int
	copyNums = append(copyNums, nums...)
	length := len(copyNums)
	for index := 0; index < length; {
		if index == copyNums[index] {
			index++
			continue
		}
		if index < copyNums[index] { //和后面元素交换
			swap(&copyNums, index, copyNums[index])
		} else { //签名的元素都是排好序的
			res = nums[index]
			break
		}
		//交换完之后 仍需比较当前下标与值是否相等
	}
	return res
}

//如果不允许改变数组的值,还是用哈希更快

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值