LeetCode summary, leetcode

Source: Internet
Author: User

LeetCode summary, leetcode
Bit operations have always been a required topic for programming and interviews. However, there are not many bit operations in the interview. The main reason is that bit operations are too skillful. In many cases, it is difficult to come up with bit operations in a short time, therefore, it takes a little time to answer questions for the interview. In LeetCode, there are the following bit operations:
Single Number
Single Number II
Divide Two Integers
Pow (x, n)

Let's talk about Single Number first. This should be the only question in LeetCode that is solved purely by bitwise. The requirement of the question itself is to find the only integer that appears once in the array, and the rest will appear twice. Here we take advantage of the exclusive or property in bitwise operations, that is, if two identical numbers are exclusive or 0 will be obtained, and the exclusive or original number of any number and 0 will be obtained. By taking advantage of the above properties, we only need to merge the elements in the array one by one, because two of them will offset each other, and only the integer that appears once will be included in the end, so we can fix it.

For Single Number II, the above method cannot be used, because the unique or nature cannot be used after three times, so another method must be used for this question. The algorithm counts the number of times each bit appears 1. Because other elements appear three times, the number of 1 on these bits will eventually be a multiple of 3. If we perform the remainder 3 for each bit of the statistical result, the remaining result will be the element that appears once. This method is common for k times, including the Single Number above. However, there is no pure bit operation method.

Next, let's take a look at the application of bitwise operations in Divide Two Integers and Pow (x, n, this is mainly because any integer can be used to represent the properties of a group of linear combinations based on the power of 2, that is, num = a_0 * 2 ^ 0 + a_1 * 2 ^ 1 + a_2 * 2 ^ 2 +... + a_n * 2 ^ n. For Divide Two Integers, based on the above properties and the one-digit left shift is equivalent to multiplying by 2, we first let the divisor move left until it is greater than the Divide to get a maximum base. Next, we try to subtract this base every time. If possible, we add 2 ^ k to the result, and then the base continues the right shift iteration until the base is 0. Because the number of iterations of this method is based on the power of 2 until the result is exceeded, the time complexity can reach O (logn), that is, the number of bits. Pow (x, n) also uses the same method to divide power n into the base of power 2, and then perform the following squares and then sum. This method is a practical method in numerical calculation, but you must be familiar with it.

This article introduces several questions about bit operations in LeetCode. Although there are few questions about bit operations, it still makes a lot of Ideological significance. If you encounter a solution that can propose bit operations during the interview, you can still add a lot of points, therefore, bitwise operations are a key weapon in some cases.
What is leetcode?

There are many programming and interview questions, which can be compiled and run online. It is difficult. If you can do it all by yourself, it is very helpful for large companies. I just did the questions there.

C ++ returns a string? Why have I tested both dev c ++ and vc60? But cannot it be used in leetcode?

The Runtime Error does not mean that your program cannot be compiled, but that your program encountered an Error during running. This error is generally caused by out-of-bounds array access, stack overflow, and illegal memory access.

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.