[algorithm] bit operation two

Source: Internet
Author: User

Topic One:

Given an array of shaped arr and an integer greater than 1 K. It is known that only 1 numbers in arr appear odd times, and the other numbers appear even several times, so return the odd number of occurrences.

The time complexity is O (N) and the additional space complexity is O (1).

Ideas:

The result of an integer n with 0 xor is n, the result of an integer n with an integer n is 0. So first apply for an orthopedic variable, which is written as EO. With EO and each number XOR (the current number of eo=eo^), the last EO value is the number that has an odd number of occurrences.

An XOR operation satisfies the commutative law and the binding law.

 Public Static void printOddTimesNum1 (int[] arr) {        int eO = 0;          for (int  cur:arr) {            ^= cur;        }        System.out.println (EO);    }
Topic Two:

Given an array of shapes arr, there are two numbers that appear odd several times, and the other numbers appear even several times. Print these two numbers.

Ideas:

If only A and B appear odd number of times, then the final XOR result EO must be a^b. Therefore, if there are two occurrences of an odd number in the array, the final result EO must not be 0. Then it is certainly possible to find a bit bit that is not equal to 0 on the 32-bit integer eo, assuming that k is not equal to 0. EO is not equal to 0 on the K-bit, stating that K-bits A and B must have one 1 and the other is 0. Next, set a variable to Ehasone, and then iterate through the array again. In this traversal, the Ehasone only has an integer that is 1 on the K-bit and the other number is ignored. Then at the end of the second traversal, Ehasone is one of a or B, and Eo^ehasone is another number.

 Public Static voidPRINTODDTIMESNUM2 (int[] arr) {        intEO = 0, Eohasone = 0;  for(intCurnum:arr) {EO^=Curnum; }        intRightone = EO & (~eo + 1);  for(intCur:arr) {            if((cur & rightone)! = 0) {Eohasone^=cur; }} System.out.println (Eohasone+ "" + (EO ^eohasone)); }
Topic Three:

Given an array of shaped arr and an integer greater than 1 K. It is known that only 1 numbers in arr have occurred 1 times, and the other numbers have been k times, please return only 1 occurrences.

Ideas:

Two k-binary numbers A and B, the result of no carry addition on the I bit is (a (i) +b (i))%k. So if K is the same K-binary number without carrying the sum, the result must be that each one is 0 K-binary number.

First set up a variable eo, which is a 32-bit K-binary number, and each position is 0. then traverse arr to convert each integer that is traversed to a K-number, and then add the non-rounding to the EO. At the end of the traversal, converting the 32-bit K-eores into a decimal integer is the final result. Because K is the same K-binary number is not added, the result must be every one on the 0 K-number, so only one occurrence of the number will be left.

 Public Static intOncenum (int[] arr,intk) {int[] EO =New int[32];  for(inti = 0; I! = arr.length; i++) {Setexclusiveor (EO, Arr[i], k); }        intres =Getnumfromksysnum (EO, k); returnRes; }    //Adds the current number corresponding to the K-binary to the corresponding position of the array     Public Static voidSetexclusiveor (int[] EO,intValueintk) {int[] Curksysnum =Getksysnumfromnum (value, k);  for(inti = 0; I! = eo.length; i++) {Eo[i]= (Eo[i] + curksysnum[i])%K; }    }    //Convert value to 32-bit K-binary     Public Static int[] Getksysnumfromnum (intValueintk) {int[] res =New int[32]; intindex = 0;  while(Value! = 0) {Res[index+ +] = value%K; Value= value/K; }        returnRes; }    //get the final result based on the remaining digits in the last array     Public Static intGetnumfromksysnum (int[] EO,intk) {intres = 0;  for(inti = eo.length-1; I! =-1; i--) {res= Res * k +Eo[i]; }        returnRes; }

[algorithm] bit operation two

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.