Bit arithmetic has been programmed and interviewed in a subject that must be prepared. But now in the interview about bit operations appear not much, the main reason is also bit operation too survey skills, many times it is difficult to think out in a short time, so as the interview topic seems a bit too much time. There are several topics about bitwise operations in Leetcode:
Single number
Single II
Divide two integers
Pow (x, N)
To say single number first, this should be the only leetcode problem that is solved purely by bitwise arithmetic. The title itself requires that you find the only integer that appears once in the array, and that the other occurs two times. Here we take advantage of the XOR nature of the bitwise operation, that is, two identical numbers are different or will get 0, and any number is different from 0 or the original number. Using the properties above, as long as the elements in the array of one by one different or up, because the occurrence of two will cancel each other, the end will be only one occurrence of the integer, so it's done.
For single number II, there is no way to do this, because three times you cannot use the different or the nature, so this topic has to use another method. The algorithm counts the number of occurrences of 1 per bit, because the other elements appear three times, so the number of 1 on these bits will be a multiple of 3. If we take each of the statistics to 3, the rest of the results will be left with the element that appears once. This method is universal for the appearance of K, including the single number above, but there is no pure bit arithmetic.
Next look at the application of bitwise operations in the Divide two integers and Pow (x, N), mainly by using any integer that can be expressed as the property of the linear combination of a group of bases with a power of 2, i.e. num=a_0*2^0+a_1*2^1+a_2*2^2+ ... +a_n*2^n. For Divide two integers based on the nature of the above and the left one equivalent of multiplying by 2, we first let the divisor move left until it is greater than dividend to get a maximum base. Then each time we try to subtract the base, if we can then add 2^k to the result, and the base continues to move the iteration right until the base is 0. Because the iteration number of this method is 2 power until the result is exceeded, the time complexity can reach O (Logn), which is the number of bits. and Pow (x, N) uses the same method to decompose the power number n into the base of the power number of 2, then the sum of squares then squared. This method is regarded as a practical method in numerical calculation, or it should be mastered skillfully.
This summary introduces a few of the leetcode, although the number of small, but the mind is still very meaningful, if the interview can be put forward the solution of the bit operation or can add a lot of points, so bit operation in some topics or a key weapon.
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