一个数组中找重复数
来源:互联网 发布:网络举报赌博有用吗 编辑:程序博客网 时间:2024/05/09 10:54
一个大小为n的数组,里面的数都属于范围[0, n-1],有不确定的重复元素,找到至少一个重复元素,要求O(1)空间和O(n)时间。
#include <stdio.h>const int NO_REPEAT_FLAG = -1;int FindRepeatNumberInArray(int *a, int n){for(int i = 0; i < n; i++){int nRealIndex = a[i] >= n ? a[i] - n : a[i];if (a[nRealIndex] >= n) //这个位置上的值大于n说明已经是第二次访问这个位置了return nRealIndex;elsea[nRealIndex] += n;}return NO_REPEAT_FLAG; //数组中没有重复的数}void PrintfArray(int a[], int n){for (int i = 0; i < n; i++)printf("%d ", a[i]);putchar('\n');}int main(){ const int MAXN = 10;//int a[MAXN] = {2, 4, 1, 5, 7, 6, 1, 9, 0, 2};int a[MAXN] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 0};printf("数组为: \n");PrintfArray(a, MAXN);int nRepeatNumber = FindRepeatNumberInArray(a, MAXN);if (nRepeatNumber != NO_REPEAT_FLAG)printf("该数组有重复元素,此元素为%d\n", nRepeatNumber);elseprintf("该数组没有重复元素\n");return 0;}
0 0
- 一个数组中找重复数
- 【C语言】简单思路找一个数组中重复次数最多的数
- 找数组中重复元素
- 找一个数组中出线两次的数
- 找一个数组中第K大的数
- 找一个数组中出现奇数次的那个数
- 找出一个数组中没重复出现过的数
- 找出一个数组中重复次数最多的数
- 在一个数组中找两个数,使得它们的和为一个指定的数
- leetcode 287. Find the Duplicate Number,数组中找重复的数
- 面试题:数组中找重复元素
- 找数组中唯一重复的元素
- 数组中找重复的数字
- java实现找数组中重复数字
- 数组中重复的数
- 数组中重复的数
- 数组中重复的数
- 数组中重复的数
- PAT 1088. Rational Arithmetic (20)
- eclipse点击clean或build后不能自动生成apk问题解决
- iOS输入框随键盘滑动的另类实现
- 二叉搜索树ADT_BSTree
- Bentley RM Bridge Advanced+ V8i 08.11.28.02 - 发现者软件网
- 一个数组中找重复数
- 混响音效
- 第四届福建省ACM程序设计大赛解题报告(未完待续)
- (java)Binary Tree Level Order Traversal II
- DEV控件ComboBoxEdit附加下拉数据列方法(sqlhelper类自已找)
- Android——AndroidStudio字体大小设置(Log日志区域)
- 1072. Gas Station (30)
- 3【学校教学系统】获取主界面
- mysql备份与恢复实践案例