数组中重复的数字
2016-06-04 22:51
253 查看
/* *在一个长度为n的数组里的所有数字都在0到n-1的范围内。 *数组中某些数字是重复的,但不知道有几个数字是重复的。 *也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 *例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出 *是重复的数字2或者3 */ // 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 /* *判断数组的下标i和numbers[i]是否相等,若想等,则i++,这是第一次遇到数字i *否则,看numbers[i]是否为numbers[numbers[i]],若想等则已找到重复数字, *否则和numbers[i]交换,这样要找到重复数字,肯定是已经第二次遇到了这个数字 */ bool duplicate(int numbers[], int length, int* duplication) { assert(numbers); assert(duplication); if (length <= 1) { return false; } int i = 0; while (i < length) { if (numbers[i] == i) { ++i; } else { if (numbers[numbers[i]] == numbers[i]) { *duplication = numbers[i]; return true; } else { swap(numbers[numbers[i]], numbers[i]); } } } return false;//没有重复数字,正好是0,1,...n-1 }
相关文章推荐
- C# 实现IAccessible学习(三)
- 基于IBM Bluemix的数据缓存应用实例
- Python 学习点滴
- 详解spring中每个jar包的作用
- L2-012. 关于堆的判断
- Bootstrap框架基础
- java 实现n个整数的全排列
- 在Android开发中如何避免内存泄露
- Dealloc 时取 weak self 时崩溃
- 通过实现Linux下的进度条引发的一系列问题
- 63. Unique Paths II
- 自定义HtmlHelper控件
- 别轻言放弃
- C语言之typedef
- 【备忘】C#语言基础-2
- 转 Python爬虫入门七之正则表达式
- ajax操作XML
- UNIX环境高级编程-第3章 文件I/O
- 删除字符串中出现次数最少的字符
- Myeclipse+oracle分页查询