[Sword Finger Offer]-repeating numbers in the array

Topic requirements:

All numbers in an array of length n are in the range of 0 to n-1. Some numbers in the array are repeated, but I do not know how many numbers are repeated. I don't know how many times each number repeats. Please find any duplicate number in the array. For example, if the input array of length 7 is {2,3,1,0,2,5,3}, then the corresponding output is the first repeated number 2.

Resolution:

Method 1:
Sort the array first, finding duplicate numbers from the sorted array is an easy task, but the time complexity O (nlogn) is very high

Method 2:
Because all numbers in the array of length n are in the range of 0 ~ n-1, if there are no repeated numbers, then the number i will appear in the position of the subscript i after the array is sorted. However, due to repeated numbers, there may be multiple numbers in some locations, and there may be no numbers in some locations.
Let's take a concrete example to describe our method
such as array {2 , 3, 1, 0, 2, 5, 3}. The first number is 2! = Subscript 0, so swap 2 to the subscript 2 position.
Insert picture description here
i ++, continue to compare, the second number 3! = subscript 1, so exchange 3 to the position of subscript 3 to
Insert picture description here
i ++, continue to compare, the third number 2 == subscript 2, do not exchange.
i ++, continue to compare, the fourth number 3 == subscript 3, do not exchange. ,
I ++, continue to compare, the fifth number 2! = Subscript 4, compare number [4] == number [numver [4]]. So a duplicate value
code implementation was found:

bool duplicate(int number[], int length, int* duplicate)
{
	if (number == nullptr || length <= 0)
	{
		return false;
	}
	for (int i = 0; i < length; i++)
	{
		if (number[i]<0 || number[i]>length - 1)
		{
			return false;
		}
	}
	for (int i = 0; i < length; i++)
	{
		while (i != number[i])
		{
			if (number[i] == number[number[i]])
			{
				*duplicate = number[i];
				return true;
			}
			int tmp = number[i];
			number[i] = number[tmp];
			number[tmp] = tmp;
		}
	}
	return false;
}	

Topic improvement:

Arrays that cannot be entered based on the above questions

Resolution:

Method 1:
We create an auxiliary array with a length of n + 1, and then copy each number of the original array to the auxiliary array one by one, and copy the number with the value m to the position marked with m. But the space complexity of doing so is O (n). So we came up with another method

Method 2: It
is still explained with a specific array, such as array {2,3,5,4,3,2,6,7}. There are 8 numbers. The middle 4 divides the array into two parts, one section is 1-4, and the other section is 5-7.
Insert picture description here
The number in 1-4 is greater than the latter, so the repeating number must be between 1-4.
Then divide the middle 2 into two groups,
Insert picture description here
the number in 1-2, 3-4 3-4 is greater than the former, so the repeat number must be between 3-4.
Then count the number of times these two numbers appear in the array.

The code is implemented as follows:

int getcount(int* number, int length, int start, int end)
{
	if (number == nullptr)
		return 0;
	int count = 0;
	for (int i = 0; i < length; i++)
	{
		if (number[i] >= start && number[i] <= end)
		{
			count++;
		}
	}
	return count;
}
int getduplicate(int* number, int length)
{
	if (number == nullptr || length <= 0)
		return -1;
	int end = length - 1;//7
	int start = 1;//1
	while (end >= start)
	{
		int middle = ((end - start) >> 1) + start;//4
		int count = getcount(number, length, start, middle);
		if (end == start)
		{
			if (count > 1)
				return start;
			break;
		}
		if (count > (middle - start + 1))//5>4 所以在1-4里面再划分
			end = middle;
		else
			start = middle;
	}
	return -1;
}

This method is similar to binary search, the function getcount is called O (logn) times, each time needs O (n) time, so the time complexity is O (nlogn). But the space complexity is O (1). This is a time-for-space algorithm.

Published 98 original articles · won praise 9 · views 3660

Guess you like

Origin blog.csdn.net/qq_43412060/article/details/105254115