My answer:
public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int n) { if (n == 0) { return 0; } int num1Bit = 0; while (n != 0) { if ((n & 1) == 1) { num1Bit ++; } n = n >>> 1; } return num1Bit; } }
Hello, welcome to Yizhe's Blog. I am Yizhe Liu, graduated from University of Arizona, with Master degree, major in Electrical and Computer Engineering. Actually, I am software guy. In my blog, many posts are about the Leetcode questions. I will post my ideas and implementations, which are accepted by Leetcode. If you have different opinions, please leave comment. I am happy to discuss with you. So lets start rocking them!!!
public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int n) { if (n == 0) { return 0; } int num1Bit = 0; while (n != 0) { if ((n & 1) == 1) { num1Bit ++; } n = n >>> 1; } return num1Bit; } }
No comments:
Post a Comment