There are an array of repeating elements is a very important idea

Yesterday was doing 17 years on the subject of Donghua re-examination machine, encountered such a problem:

(4)
has n (n large) ranges from 0 to 32767 number, including a large number of repeating, the main function has been read into the data array, write function fun, after discarding duplicates digital calculation, left at a few numbers.
fun function is the function: two incoming parameter, a is an array Data, is a value of n has been calculated, returns the number of remaining after discarding duplicates of digital numbers. For example, the input program is run:
511,313 the process output: 2
Requirements: Please measure the time complexity and space complexity, try to design an efficient algorithm. Please introduce their own algorithms on top of the comments section prog1.c.
Note: Some of the source file exists prog1.c. Do not alter any content main function and other functions, just fill in a number of statements that you write in braces function fun of.

I start thinking is this:
first row of the array a sequence right. The initial count of 1, as at least one array having at least a different number. Then traversing an ordered array, if the current element and after a range of elements, then the count is incremented, otherwise unchanged. code show as below:

int flag = 0; 
	int i, j, temp, count = 1;
	
	for (i = n - 1; i >= 1; i--) {// 给数组排序
		flag = 0;
		for (j = 0; j < i; j++) {
			if (array[j] > array[j + 1]) {
				flag = 1;
				temp = array[j];
				array[j] = array[j + 1];
				array[j + 1] = temp;
			}
		}
		if (flag == 0) {
			break;
		}
	}
	printf("排序后:\n");
	for (i = 0; i < n; i++)
		printf("%d ", array[i]);
	printf("\n");
	
	for (i = 0; i < n - 1; i++) {// 计数
		if (array[i] != array[i + 1]) {
			count++;
		}
	}

Although this method is said to solve the problem, but a little high time complexity [O (n2)], I thought for a long time could not find a way to solve, then went and looked at seniors code. I learned from the seniors a good idea:

In Flag preparing an array of tags, the number of non-zero value, the array element tag array subscript casual working on behalf of the array represents a stem that appear in the array subscript number non-zero value, for example, In Flag [20 is] =
. 3 Representative title dry the array of three numbers 20, flag [21] = 0 casual working on behalf of the array 21 does not occur. As a result only flag an array of statistics can be non-zero elements

code show as below:

int flag[32768] = {0};
	int i, count = 0;
	
	for (i = 0; i < n; i++) {
		flag[array[i]]++;
	}

	for (i = 0; i < 32768; i++) {
		if (flag[i] != 0) {
			count++;
		}
	}

Not only a lot of the code concise, but also reduced the time complexity of O (n), and this code is a ratio, it is my low burst.
Reference: https://blog.csdn.net/wy_97/article/details/88616151

Published 19 original articles · won praise 5 · Views 4120

Guess you like

Origin blog.csdn.net/qq_41409120/article/details/103946512