Using Java to implement XOR or operation

Source: Internet
Author: User

Recently saw a lot of blog Park graduates interview BAE Success experience, heartfelt admiration and full of sigh: pay always have return. So I am also itching to find a few interview topics to do. Inadvertently saw the problem.

An array holds 2n+1 integers, with n number appearing 2 times, 1 numbers appearing 1 times to find out what the number is 1 times?

I thought for a long time did not think out, I am very stupid, Baidu has the answer, said as long as all the number of different or once can. Suddenly, I want to implement it in Java immediately. But the result is not right at all. Always thought it was their own wrong code, but also Baidu the next different or the symbol ^ in the end is right, the symbol is right, but the degree Niang said that Java does not have binary type, the only binary with the edge, only the int into a string to reluctantly represent the binary number. How can this be good, for me this kind of rookie, think why not define a binary class? After several twists and turns, finally came out!

1  PackageCom.test.main;2 3  Public classBinary {4     5     6     //define a binary instance7     PrivateString binary;8     //receive int type, convert to string;9Binary (inta) {Ten          This. binary=integer.tobinarystring (a); One     } A     //receives a binary of type string, used to construct a binary object - Binary (String s) { -          This. binary=s; the     } -      -      Publicbinary xor (binary x) { -String anotherbinary=x.binary; +String thisbinary= This. Binary; - String temp; + String result; A         intlength; atLength=thisbinary.length ()-anotherbinary.length (); -         //Keep Thisbianry ' s length is not shorter than anotherbnary; -         if(length<0){ -temp=thisbinary; -thisbinary=anotherbinary; -anotherbinary=temp;
Length=-length; in } - to //intercept longer strings and more. + if(length>0){ -Result=thisbinary.substring (0, length); the } * Else{ $Result= "";Panax Notoginseng } - for(intI=0;i<anotherbinary.length (); i++){ the //if the same number of positions is the same, fill 0, otherwise 1 + if(Thisbinary.charat (i+length) = =Anotherbinary.charat (i)) Aresult+= "1"; the Else{ +result+= "0"; - } $ } $ //Constructing binary Objects -Binary b=NewBinary (result); - the returnb; - Wuyi } the}

The above for the definition of the binary class, can achieve the XOR operation, in fact, the other and and the operation can be achieved, there is no cumbersome.

1      Public Static voidMain (string[] args) {2         int[] arr={1,1,2,2,3,3,4,4,4,4,5,5,6,6,7,7,8,8,8};3Binary sum=NewBinary (arr[0]);4          for(inti=1;i<arr.length;i++){5Sum=sum.xor (NewBinary (Arr[i]));6         }7 System.out.println (sum.binary);8 9}

The code I tested above, that is, as long as the array contains an even number of integers, and only one is an odd number of integers, you can find this odd numbers, and finally print out the odd binary form, in fact, I should write the method, the binary conversion to int. The implementation should be simpler.

Finally add a sentence, in fact, the efficiency is very low, to each bit of a string operation, which also has string+ "" statement is actually very inefficient, I just to achieve and realized, actually or with HashMap record each number of times, the odd number of times to carry out on the can, If you are too slow, use C language directly!

Using Java to implement XOR or operation

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.