面试题3:找出数组中重复的数字。
在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。
示例 1:
输入:
[2, 3, 1, 0, 2, 5, 3]
输出:2 或 3
限制:
2 <= n <= 100000
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解法一:[原地置换,时间空间100%]
利用哈希的特性,但不需要额外的存储空间。 因此时间复杂度为O(n),不需要额外空间,空间复杂度o(1)!
把当前序列当成是一个下标和下标对应值是相同的数组;
遍历数组,判断当前位的值和下标是否相等: 2.1. 若相等,则遍历下一位; 2.2. 若不等,则将当前位置i上的元素和a[i]位置上的元素比较:若它们相等,则成功!若不等,则将它们两交换。换完之后a[i]位置上的值和它的下标是对应的,但i位置上的元素和下标并不一定对应;重复2.2的操作,直到当前位置i的值也为i,将i向后移一位,再重复2.。
如果没有重复数字,那么正常排序后,数字i应该在下标为i的位置,所以思路是重头扫描数组,遇到下标为i的数字如果不是i的话,(假设为m),那么我们就拿与下标m的数字交换。在交换过程中,如果有重复的数字发生,那么终止返回ture
/*
遍历数组 documentsdocumentsdocuments ,设索引初始值为 i=0 :
若 documents[i]=i : 说明此数字已在对应索引位置,无需交换,因此跳过;
若 documents[documents[i]]=documents[i] : 代表索引 documents[i] 处和索引 i 处的元素值都为 documents[i] ,即找到一组重复值,返回此值 documents[i]documents[i]documents[i] ;
否则: 交换索引为 i 和 documents[i]的元素值,将此数字交换至对应索引位置。
若遍历完毕尚未返回,则返回 −1 。
作者:Krahets
链接:https://leetcode.cn/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof/solutions/96623/mian-shi-ti-03-shu-zu-zhong-zhong-fu-de-shu-zi-yua/
来源:力扣(LeetCode)
*/
class Solution {
public:
int findRepeatDocument(vector<int>& documents) {
for (int i = 0; i < documents.size();) {
if (documents[i] == i) {
i++;
continue;
}
if (documents[documents[i]] == documents[i]) {
return documents[i];
}
swap(documents[i], documents[documents[i]]);
}
return -1;
}
};
解法二:使用map
,和题51类似
因此时间复杂度为O(n),不需要额外空间,空间复杂度o(n)
class Solution {
public:
int findRepeatNumber(vector<int>& nums) {
if(nums.empty())
return -1;
map<int,int> hashmap;
for(size_t i = 0; i < nums.size(); ++i){
hashmap[nums[i]]++;
}
for(int i = 0; i < nums.size();++i){
if(hashmap[nums[i]] > 1)
return nums[i];
}
return 0;
}
};
方法三:不修改数组且要求空间复杂度为O(1),时间复杂度为O(n)
// 面试题3(二):不修改数组找出重复的数字(和T61思路类似)
// 题目:在一个长度为n+1的数组里的所有数字都在1到n的范围内,所以数组中至
// 少有一个数字是重复的。请找出数组中任意一个重复的数字,但不能修改输入的
// 数组。例如,如果输入长度为8的数组{2, 3, 5, 4, 3, 2, 6, 7},那么对应的
// 输出是重复的数字2或者3。
#include <iostream>
int countRange(const int* numbers, int length, int start, int end);
// 参数:
// numbers: 一个整数数组
// length: 数组的长度
// 返回值:
// 正数 - 输入有效,并且数组中存在重复的数字,返回值为重复的数字
// 负数 - 输入无效,或者数组中没有重复的数字
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;
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;
}
// ====================测试代码====================
void test(const char* testName, int* numbers, int length, int* duplications, int dupLength)
{
int result = getDuplication(numbers, length);
for(int i = 0; i < dupLength; ++i)
{
if(result == duplications[i])
{
std::cout << testName << " passed." << std::endl;
return;
}
}
std::cout << testName << " FAILED." << std::endl;
}