In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.