[11 of the vernacular classical algorithm series] An interesting Google interview question-[solution 2]

Source: Internet
Author: User

Weibo http://weibo.com/morewindowsis available. Welcome to the Forum.

This article series address: http://blog.csdn.net/MoreWindows/article/category/859207

In the last article "11 interesting Google interview questions in the vernacular classical algorithm series", I gave a detailed explanation of an interesting Google interview question. I used the base sorting method in O (n) the time complexity and the space complexity of O (1) fulfill the requirements of the question. After the article is published, fengchaokobe, a netizen, provides another solution in the comments. See.

Text Version:

Int repeat (int * a, int N) {for (INT I = 0; I <n; I ++) {if (a [I]> 0) // judgment condition {if (a [A [I] <0) {return a [I]; // it has been marked as a negative value, repeated} else {A [A [I] =-A [A [I]; // record as negative} else // At this time | A [I] | The representative value has already appeared once {if (a [-A [I] <0) {return-A [I]; // found again} else {A [-A [I] =-A [-A [I] ;}} return-1; // no duplicates in the array}

The following example describes how to use a negative access flag:

Set int A [] = {1, 2, 1}

Step 1: because a [0] is equal to 1 and greater than 0, first judge whether a [A [0] is smaller than 0, this is the second element with the subscript of 1, indicating that we have found the repeated element. Otherwise, a [A [0] is taken as a negative value. A [1] =-A [1] =-2.

Step 2: because a [1] is equal to-2, first judge whether a [-A [1] retrieves a [2] is less than 0. If it is less, this is the second element with the subscript of 2, indicating that we have found the repeated element. If not, take a [-A [1] as a negative, a [2] =-A [2] =-1.

Step 3: because a [2] is equal to-1, determine whether a [-A [2] is smaller than 0, because a [1] has been reversed in the first step, it is proved that this is the second element accessing the subscript of 1. Return-A [2] directly.

 

This method uses the negative value to determine whether the elements are repeatedly accessed. As the netizen feng002 said, when the first 0th elements in the array are 0 and there is only 0 duplicates in the data, the correct solution cannot be found. As long:

Const int maxn = 5;

Int A [maxn] = {0, 1, 2, 3, 0 };

If this set of data is tested, we will find that this method cannot judge that 0 is a recurring element. Shows the running result:

 

Although this algorithm is flawed, we can follow the idea of this algorithm-the reason why this algorithm is used to obtain the negative is that according to the question condition, the data range in the array is [0, n-1], therefore, you can determine whether the element is greater than 0 to determine whether the element is unaccessed data or accessed data. However, this algorithm is flawed because the negative value is invalid. It is also easy to improve-adding N instead of negative values. In this way, you can determine whether the element is greater than or equal to n to determine whether the element is unaccessed data or accessed data. The complete code is as follows:

// Google interview question // an array of N values in the range of [0, n-1]. If there is an uncertain repeating element, find at least one repeating element, requires O (1) space and O (n) time. // By morewindows (http://blog.csdn.net/MoreWindows) # include <stdio. h> const int no_repeat_flag =-1; int findrepeatnumberinarray (int * a, int N) {for (INT I = 0; I <n; I ++) {int nrealindex = A [I]> = n? A [I]-N: A [I]; if (a [nrealindex]> = N) // if the value is greater than N, return nrealindex; elsea [nrealindex] + = N;} return no_repeat_flag is already accessed for the second time; // no duplicates in the array} void printfarray (int A [], int N) {for (INT I = 0; I <n; I ++) printf ("% d", a [I]); putchar ('\ n');} int main () {printf ("11 interesting Google interview question solution for vernacular classical algorithm series 2 \ n"); printf ("-- by morewindows (http://blog.csdn.net/MoreWindows) -- \ n "); const int maxn = 10; // int A [maxn] = {2, 4, 1, 5, 7, 6, 1, 9, 0, 2}; int A [maxn] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 0}; printf ("array: \ n"); printfarray (A, maxn); int nrepeatnumber = findrepeatnumberinarray (A, maxn ); if (nrepeatnumber! = No_repeat_flag) printf ("this array has repeated elements. This element is % d \ n", nrepeatnumber); elseprintf ("this array has no repeated elements \ n "); return 0 ;}

Running result:

Just like the last article titled the eleven interesting Google interview questions of the vernacular classical algorithm series, the core code of the algorithm is still about five lines. The complexity of the time space also meets the requirements of the question.

 

 

I believe this article shows that the conversion of thinking is very useful for finding a suitable algorithm.

 

In addition, pay attention to writing the code. By comparing the repeat () function in the article with findrepeatnumberinarray (), we can find that it is necessary to make the code concise. If I have completed all the interview questions during the Google interview, what does the interviewer feel about the two codes? This is precisely why many children's shoes are confused after the interview.

 

Address of the Classical Vernacular algorithm series:

Http://blog.csdn.net/MoreWindows/article/category/859207

Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/8212446

Welcome to Weibo: http://weibo.com/MoreWindows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.