diff --git a/number-of-1-bits/ymir0804.java b/number-of-1-bits/ymir0804.java new file mode 100644 index 0000000000..fe540587dd --- /dev/null +++ b/number-of-1-bits/ymir0804.java @@ -0,0 +1,18 @@ +class Solution { + public int hammingWeight(int n) { + int result = 1; + if(n <= 2) { + return result; + } + while (true) { + if (n % 2 == 1) { + result++; + } + n /= 2; + if(n <= 2) { + break; + } + } + return result; + } +}