找到重复的数

/*
  http://weibo.com/1915548291/yDjra5ajO#1348465113389
  一个大小为n的数组,里面的数都属于范围[0, n-1],有不确定的重复元素,
  找到至少一个重复元素,要求O(1)空间和O(n)时间。
 */

#include <stdlib.h>
#include <assert.h>
#include <time.h>

#define NELEMS(a) (sizeof(a)/sizeof(a[0]))
void swap(int *x, int *y)
{
  int tmp = *x; *x = *y; *y = tmp;
}

/*
  Term: "right place": a[i] = i, "wrong place": a[i] != i
  the while loop executes at most n times, because each execution
  puts an element from the "wrong place" into the "right place",
  and there are at most n elements at "wrong place".
  Side effect: parameter a is changed
*/
int find_duplicate(int a[], int n)
{
  int i;
  for(i = 0; i < n; i++) {
    /* a[0]..a[i-1] are in right place */
    while(a[i] != i) {
      if (a[i] == a[a[i]])
	return a[i];
      else
	swap(&a[i],&a[a[i]]);
    }
  }
  /* a[0]..a[n-1] are in right place
     no duplicate one */
  return -1;
}

void shuffle(int a[],int n)
{
  while(--n > 0) {
    int k = rand() % (n+1);
    swap(&a[k],&a[n]);
  }
}

int main()
{
  /* simple test */
  int i;
  srand(clock());
  for(i = 0; i < 10000000; i++) {
    int a[] = {0,1,2,3,4,5,6,7,8,8};
    shuffle(a,NELEMS(a));
    assert(8 == find_duplicate(a,NELEMS(a)));
  }
  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值