algorithm--to Find missing integers

Source: Internet
Author: User

first, Preface

Recently the IQ continues to drop the line, vaguely a kind of early to make Alzheimer's disease feeling, Idol play to see more is easy IQ continued to drop line, the previous whole son concerned about an algorithm of the public number, today also finally picked up a can read, feel very up posture, the whole article look down feel oneself have a great ascension, as if almost to see Understand.

The following is a link to the original text, in order to prevent the link from being destroyed, to maintain the rising posture also find peace, granny, cute and charming villain character here decided to open an essay to organize the content and comments inside the code, and so On.

Please poke this http://blog.jobbole.com/106521/in the Original.

second, Please look at the text First Question:Please listen to the question,to turn off the elephant in the freezerThere are 99 non-repeating positive integers in an unordered array, ranging from 1 to 100, except for a missing integer. How do I find this missing integer? Solution One:

Create a hashmap with a key of 1 to 100 and a value of 0. Then iterate through the entire array, and each time an integer is read, it finds the corresponding key in the HashMap and adds one to its Value. The woman stops at this step, because the grandmother will use javascript, so the grandmother intends to create a new group

Because an integer is missing from the array, there must be a value of 99 keys equal to 1, and the remaining key corresponds to 0. Iterate through the modified HashMap and find the key with a value of 0.

Assuming that the array length is N, then the time complexity of the solution is O (2N), and the spatial complexity is O (N). space complexity and time complexity This is a woman without a brain copy, grandma intends to have time to kneel down to Baidu

Analysis:

The solution is optimal in time, but it opens up extra space. Consider how to reduce the complexity of space?

Solution Two:

Sort the array elements first, and then iterate through the array to see if the values of any two adjacent elements are contiguous. If discontinuous, the missing integer is the one you are looking for, and if all is contiguous, the missing integer is either 1 or 100. This woman think it is too complicated, mainly the old woman is not going to want to break the head to sort, Resolute shake abandon

Assuming that the array length is N, if the time complexity is O (n*logn) sorting algorithm, then the time complexity of the solution is O (n*logn), the spatial complexity is O (1).

Analysis:

This solution does not open up additional space, but the complexity of the time is Large. Think about whether it's time and space to optimize?

Solution Three:

Very simple and efficient method, first calculate the 1+2+3....+100 and then subtract the elements in the array, and finally get the difference, is the only missing integer. See here the old lady seems to feel very familiar, as if this question before from where seen, this solution seems to have been deeply doubted by a grandmother

Assuming that the array length is n, then the time complexity of the solution is O (N), and the spatial complexity is O (1).

Analysis:

For arrays that do not have duplicate elements, the solution is optimal in both time and space. Below please consider the topic extension ...

the Second Question:Listen to the question, there are several positive integers in an unordered array, ranging from 1 to 100, where 99 integers appear even several times, and only one integer appears odd times (such as 1,1,2,2,3,3,4,5,5), how can I find this integer that appears odd number of times?

tip: using an XOR operation , the same result is 0 at the time of operation, with a different result of 1.

Solution:

Iterate through the entire array, doing the XOR operation in Turn. Because the XOR is the same as 0, the difference is 1, so all occurrences of an even number of integers will be offset into 0, and only the odd number of occurrences of an integer will be left.

Assuming that the array length is n, then the time complexity of the solution is O (N), and the spatial complexity is O (1).

question Three:There are several positive integers in an unordered array, ranging from 1 to 100, where 98 integers appear even several times, and only two integers appear odd times (for example, 1,1,2,2,3,4,5,5), How can I find this integer that appears odd number of times?

hint: The use of divide and conquer the method (rong Grandma kneeling to baidu), the array into two parts can be returned to the above Situation.

Solution:

Iterate through the entire array, doing the XOR operation in Turn. Because the array has two integers that appear odd times, the result of the final XOR is equivalent to the XOR result of the two Integers. In this result, at least one bits is 1 (if all are 0, the two numbers are equal, and the title does not match).

For example, if the final XOR result is 5, the conversion to binary is 00000101. At this point we can choose any one is 1 bits to analyze, such as the Last. The two odd number of occurrences of the integer named A and b, if the last is 1, A and B to the binary to the bottom of the difference, it must be one of the first integer is 1, and the other integer is 0. Grandma was trying to blow her brains out. if the 1 is not the last one to do, the comment area of the large code carving can be artificial to the last one (not reflected in the code Behind)

According to this conclusion, we can divide the original array into two parts according to the bottom of the binary, part of the bottom is 1, part of the bottom is 0. Since the bottom of a and B are different, A is a part of it, and B in one part of it, there is no case where A and B are in the same part and the other part is Not.

So it's easy, and our problem goes back to the previous one, and according to the original XOR method, we can find the unique odd number of integers from each part.

Assuming that the array length is n, then the time complexity of the solution is O (N). Divide the array into two parts, do not need to use additional storage space, can be grouped by bits at the same time to do the different or operation, so the space complexity is still o (1).

third, Comment Area code collationGrandma will comment area a big code called a glass cat did some processing (with JS without a brain copy) to sort out the following code:
1<script>2     //to find the XOR value of all the numbers in the array3     functionfindoutmissingdigits (arr) {4         varresult = 0;5          for(varIinchArr) {6Result ^=arr[i];7         }8         returnresult;9     }Ten  one  a     //the return value of this method is used to differentiate two numbers, and the parameter num is the XOR value of two numbers -     functiongetshednum (num) { -         varNumshift = 1; the  -         /** - * This while is used to determine the number of NUM from right to left, which one appears 1: - * If Num's value is ten = parseint (' 1010 ', 2), The value of numshift is 1 = parseint (' 0001 ', 2) + * Execute Numshift = numshift<<1; will numshift left displacement one bit to get 2 = Numshift = parseint (' 0010 ', 2) - * until the & result is not zero for the number that makes two number difference or the result of the operation is not 1, the result of two number &numshift is 0 or 1 . +         */ a          while(num & Numshift) ==0){ atNumshift = numshift<<1; -         } -  -         returnnumshift; -     } -  in     functionfindoutMissingDigits2 (arr) { -         varA = 0; to         varB = 0; +         varresult =findoutmissingdigits (arr); -         //get the "watershed" of the array division from the XOR result of two numbers the         varShednum = Getshednum (result);//Tips: If you want to use the right shift 1 and 0 to the last one, calculate the string length of the shednum into a string, move the length to the right-1 bits *          for(varIinchArr) { $A = ((arr[i] & shednum) = = 0)? (a^arr[i]): A;Panax NotoginsengB = ((arr[i] & shednum)! = 0)? (b^arr[i]): B; -         } the         return[A, B]; +     } a  theFindoutMissingDigits2 ([1,1,2,2,3,3,4,5,6,6,7,7])//[4,5] +</script>

Iii. SummaryThe following is a large amount of information and the use of the posture of the arrangement of the woman, Grandma brain to burn out, zhi hard ~

Bit operation and its application

Decimal is converted to Binary: (assuming 63 is converted to 2 Binary) parseint (2). toString;

Binary conversion to Decimal: (assuming 101110100 is converted to 10 Binary) parseint ("101110100", 2);

XOR Operation ^: (parseint ("1011", 2) ^ parseint ("2") = = parseint ("1101", 2)//true

Right displacement >>: (parseint ("1011", 2) >> 1) = = parseint ("101", 2)//true (parseint ("1011", 2) >> 2) = = Parsei NT ("ten", 2)//true

ok, Next is the statement, if there is a large billion to see this article, see I quoted you meng's things did not indicate the source, what discomfort, please contact me, in addition, no brain copy of me if there is not much of a copy of the Million one is greatly seen, ask (a Sound) i Or2

Algorithm--find missing integers

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.