Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to solve the problem of the number of 1s in binary system by LeetCode

Shulou Source: shulou.com Published: 2022-06-01 13:08:05 09月26日 Update

This article mainly introduces LeetCode how to solve the problem of the number of 1s in the binary system, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Title

Please implement a function that inputs an integer and outputs the number of ones in the binary representation of the number. For example, representing 9 as binary is 1001 and 2 bits are 1. Therefore, if you enter 9, the function outputs 2.

Example 1: input: 000000000000000000000000000000000000000000001011 output: 3 explanation: input binary string 000000000000000000000000000000000000001011, a total of three bits are'1'. Example 2: input: 0000000000000000000000000010000000 output: 1 explanation: input binary string 000000000000000000000000000010000000, a total of one bit is'1'. Example 3: input: 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101, there are 31 bits in'1'. Train of thought

Author: jyd

Bit by bit judgment

According to the definition of and operation, if you set the binary number nn, you will have:

If nasty 1 = 0n&1=0, the rightmost bit of the nn binary is 00

If nn 1 = 1n&1=1, the rightmost bit of the nn binary is 11.

According to the above characteristics, consider the following circular judgment:

Determine whether the rightmost bit of nn is 11, and count according to the result.

Move the nn one bit to the right (this requires that the numeric nn be treated as an unsigned number, so use the unsigned right shift operation)

Steps

Initialize the quantity statistics variable res = 0.

Loop bit by bit judgment: jump out when n = 0.

Res + = nought 1: if naught 1 = 1n&1=1, then the statistic resres is added to one.

N > > = 1: move the binary digit nn unsigned one bit to the right (move the unsigned right in Java to "> >").

Returns the statistical quantity res

Skin public class Solution {/ / you need to treat n as an unsigned value public int hammingWeight (int n) {return Integer.bitCount (n);}} code public class Solution {/ / you need to treat n as an unsigned value public int hammingWeight (int n) {int res = 0; while (n! = 0) {res + = n & 1; n > = 1;} return res }} Thank you for reading this article carefully. I hope the article "LeetCode how to solve the problem of the number of ones in binary" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Binary input output article number number example symbol statistics explanation problem function quantity loop code value author interest variable number Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Docker NVidia Redmi OPPO Reno