剑指offer 面试题3:数组中重复的数字【C++版本】

题目总结与代码归档:
【剑指offer-2】题目目录【C++版本】

GitHub代码路径: GitHub

面试题3

数组中重复的数字一

题目一:
找出数组中重复的数字
在一个长度为n的数组里的所有数字都在0~n-1范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3。

使用hash表实现
# include<iostream>
using namespace std;


bool duplicate(int nums[], int length, int* duplication);

int main()
{
	int a[] = { 2,1,3,1,4 };
	int *resu = new int(0);
	duplicate(a, sizeof(a) / sizeof(int), resu);
	cout << *resu << endl;
	system("pause");
	return 0;
}

bool duplicate(int numbers[], int length, int* duplication) {
	if (numbers == nullptr || length <= 1) 
		return false;

	int *hashTable = new int[length](); // 用于下标为 numbers[] 元素

	for (int i = 0; i < length; i++) {
		int num = numbers[i];
		if (hashTable[num]){	// 如果下标为 num 中有数据表明有重复数据
			*duplication = numbers[i];	// 传出数据
			delete[]hashTable;
			return true;
		}
		else {
			hashTable[numbers[i]] = 1;
		}
	}
	delete[]hashTable;
	return false;

}
交换元素位置
# include<iostream>

using namespace std;

bool duplicate(int nums[], int length, int* duplication);

int main()
{
	int a[] = { 2,1,3,1,4 };
	int *resu = new int(-1);
	if (!duplicate(a, sizeof(a) / sizeof(int), resu))
	{
		cout << "fail" << endl;
	}
	cout << *resu << endl;
	system("pause");
	return 0;
}

bool duplicate(int numbers[], int length, int* duplication) {
	if (numbers == nullptr || length <= 1)
		return false;

	for (int i = 0; i < length; i++) {	// 数据有效检查
		int num = numbers[i];
		if (num < 0|| num > length-1){
			return false;
		}
	}

	for (int i = 0; i < length; i++) {
		while (numbers[i]!=i)	// numbers[i] 与 i 不等 一直交换 numbers[i] 和 numbers[numbers[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;

}

数组中重复的数字二

题目二:
不修改数组找出重复的数字
在一个长度为n+1的数组里的所有数字都在1~n范围内。所以数组中至少有一个数是重复的。请找出数组中任意一个重复的数字,但不能修改输入的数组。例如,输入长度为8的数组{2,3,5,4,3,2,6,7},那么对应的输出是重复的数字2或者3。

二分思想
#include <iostream>
using namespace std;

int getDuplication(const int *numbers, int length);
int countRange(const int *numbers, int length, int start, int  middle);

int main()
{
	//int a[] = { 2,3,5,4,3,2,6,7 };
	int a[] = { 1,2,3,4,5,6,7,8 };
	//int a[] = { 2,3,5,4,3,1,6,7 };
	int resu = getDuplication(a, sizeof(a) / sizeof(int));
	cout << resu << endl;

	system("pause");
	return 0;
}

int getDuplication(const int *numbers, int length) {
	if (numbers == nullptr || length <= 0)
		return -1;

	int start = 1;
	int end = length - 1;	// n+1的数组里的所有数字都在1~n范围内, 最大就为 length - 1

	while (end>=start)	// 循环条件  
	{	
		// 1、找出区间中间数据 middle
		int middle = ((end - start) >> 1) + start; // 二分思想
		// 2、统计 numbers 中 数据 在 [start,middle] 包括边界的个数 count
		int count = countRange(numbers, length, start, middle);
		if (start == middle)	// 最后总会确定到一个数
		{
			if (count > 1)// 如果出现次数 >1 ,这就是重复的数
				return start;
			else
				return -1; // 否则 数据有问题,没有重复的数

		}
		// 如果 count > middle-start+1 那么, 重复数据在 [start,middle] 之间, 于是在区间[start,middle] 重复1、2 操作
		// 如果 count <= middle-start+1  那么, 重复数据在 [middle+1,end] 之间, 于是在区间[middle+1,end] 重复1、2 操作
		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++)
	{
		int num = numbers[i];
		if (num>=start && num<=end)
		{
			count++;
		}
	}
	return count;
}

参考资料:


GitHub链接:
https://github.com/lichangke
CSDN首页:
https://me.csdn.net/leacock1991
欢迎大家来一起交流学习

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值