题目描述:
在一个长度为 n 的数组里的所有数字都在 0 到 n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字是重复的,也不知道每个数字重复几次。请找出数组中任意一个重复的数字
Input: {2, 3, 1, 0, 2, 5} Output: 2
#include<stdio.h> void swap(int *nums, int i, int j) { int t = nums[i]; nums[i] = nums[j]; nums[j] = t; } int find_same_number(int *num,int len,int *p) { int i = 0; int temp = 0; for(i = 0;i< len; ++i) { printf("num[%d] = %d ",i,num[i]); } for(i = 0;i< len; ++i) { printf("debug "); while(num[i] != i) { if(num[i] == num[num[i]]) { *p = num[i]; return 1; } //swap(num,i,num[i]); int t = num[i]; num[i] = num[t]; num[t] = t; } } return 0; } int main(void) { int nums[] = {1,2,3,4,5,6,7,3}; int target; int *p = ⌖ int ret = find_same_number(nums,8,p); printf("p = %d ",*p); return 0; }