剑指Offer-3:数组中重复的数字
来源:互联网 发布:毛泽东传 知乎 编辑:程序博客网 时间:2024/06/05 23:39
题目1:找出数组中的重复数字
在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。
Example:
如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。
问题解析:
- 元素在0到n-1的范围;
- 给出任意一个重复数字。
链接:
剑指Offer(第2版):P39
思路标签:
算法:排序、hash
数据结构:数组
解法的逐步优化:排序寻找重复——Hash表存储——数组索引的技巧性应用
解答:
1. C++
- (1) 一个简单的思路是先将数组排序,然后从头开始寻找重复数字。排序的时间复杂度为O(nlogn);
- (2) 利用hash表存储元素,若表中存在元素则找到重复数字。Hash查询时间仅用O(1),算法时间复杂度为O(n),但是需要一个哈希表,空间复杂度为O(n);
- (3) 利用元素数字都在0到n-1的范围内的特点,若不存在重复数字,则排序后的数字就在与其相同的索引值的位置,即数字i在第i个位置。遍历的过程中寻找位置和元素不相同的值,并进行交换排序。时间复杂度O(n),空间复杂度O(1)。
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) { // 空指针和空数据的情况 if (numbers == nullptr || length <= 0){ return false; } // 不符合题目要求的非法元素情况 for (int i = 0; i < length; ++i){ if (numbers[i] < 0 || numbers[i] > length -1) return false; } // 主要部分 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; }};
C++相关:
C++ 11中使用nullptr代表空指针:
- https://www.cnblogs.com/porter/p/3611718.html
题目2:不修改数组找出重复数字
在一个长度为n+1的数组里所有数字都在1~n的范围内,所有数组中至少有一个数字是重复的。请找出数组中任意一个重复的数字,但不能修改输入的数组。
Example:
如果输入长度为8的数组{2,3,5,4,3,2,6,7},那么对应的输出是重复数字2或者3。
问题解析:
- 数组长度为n,元素在1到n的范围;
- 必定至少有一个数字重复。
链接:
剑指Offer(第2版):P41
思路标签:
算法:二分查找
数据结构:数组
空间换时间:辅助数组,时间复杂度O(n),空间复杂度O(n);
时间换空间:二分查找,时间复杂度O(nlogn),空间复杂度O(1)。
解答:
1. C++
- (1) 第一种是利用辅助数组来将原数组的对应元素存储到对应的位置,需要额外的O(n)的空间;
- (2) 第二种是利用二分查找的方式。
- 我们将1~n从中间分成两份,1~m和m+1~n,遍历数组分别统计数组中出现1~m的元素的个数和出现m+1~n的个数,对于统计个数大于对应的长度时,则说明该份中出现了重复元素;
- 对出现重复元素的一份继续进行折半划分,一直到单份长度为1,但统计却含有多个元素的情况,则找到重复元素;
- 这种方法无法保证找出所有的重复数字,需要在解决前了解需求。
class Solution {public: // Parameters: // numbers: an array of integers // length: the length of array numbers // Return value: one of the duplicate numbers int getDuplication(const int* numbers, int length) { // 空指针和空数据的情况 if (numbers == nullptr || length <= 0){ return -1; } int start = 1; int end = length-1; while (end >= start){ int middle = ((end - start)>>1) + start; // (end-start)/2 +start int count = countRange(numbers, length, start, middle); if (end == start){ if (count > 1) return start; else break; } if (count > (middle-start+1)) end = middle; else start = middle + 1; } return -1; } int countRange(const int* numbers, int length, int start, int end){ if (numbers == nullptr) return 0; int count = 0; for (int i = 0; i < length; ++i){ if (numbers[i] >= start && numbers[i] <= end) ++count; } return count; }};
算法相关:
二分查找:
- https://www.zhihu.com/question/36132386
- http://blog.csdn.net/yefengzhichen/article/details/52372407
阅读全文
0 0
- 剑指offer--数组中重复的数字
- 《剑指offer》数组中重复的数字
- 剑指offer:数组中重复的数字
- [剑指Offer]数组中重复的数字
- 剑指offer:数组中重复的数字
- 剑指offer 数组中重复的数字
- [剑指offer]数组中重复的数字
- 剑指offer|数组中重复的数字
- 剑指offer-数组中重复的数字
- 剑指offer 数组中重复的数字
- 《剑指offer》数组中重复的数字
- 剑指offer-数组中重复的数字
- 剑指offer--数组中重复的数字
- 剑指offer--数组中重复的数字
- 剑指offer:数组中重复的数字
- 剑指offer:数组中重复的数字
- 剑指offer:数组中重复的数字
- 【剑指offer】数组中重复的数字
- 打开Eclipse server出现Could not create the view: An unexpected exception was thrown
- C#多线程
- 我与python第一天
- Spring MVC 到底是如何工作的?
- 刚体加力的方式
- 剑指Offer-3:数组中重复的数字
- SugarCRM_6.5Developer Documents 文档之 Introduction(译)
- 站内信
- 可查看我的个人博客
- NVIDIA显卡 Ubuntu16.04安装驱动后出现问题:Unable to determine the device handle for GPUXXX
- 假设我们的存在是一次先人模拟
- 盘点本年度九大流行前端框架
- 手机注册获取验证码
- 为什么直升机在电影里的最大作用是“坠毁”?