File tree 2 files changed +18
-1
lines changed
2 files changed +18
-1
lines changed Original file line number Diff line number Diff line change @@ -231,7 +231,7 @@ LeetCode
231
231
| 200| [ Number of Islands] ( https://leetcode.com/problems/number-of-islands/ ) | | Medium|
232
232
| 199| [ Binary Tree Right Side View] ( https://leetcode.com/problems/binary-tree-right-side-view/ ) | | Medium|
233
233
| 198| [ House Robber] ( https://leetcode.com/problems/house-robber/ ) | | Easy|
234
- | 191| [ Number of 1 Bits] ( https://leetcode.com/problems/number-of-1-bits/ ) | | Easy|
234
+ | 191| [ Number of 1 Bits] ( https://leetcode.com/problems/number-of-1-bits/ ) | [ java ] ( ./algorithms/numberof1bits/Solution.java ) | Easy|
235
235
| 190| [ Reverse Bits] ( https://leetcode.com/problems/reverse-bits/ ) | | Easy|
236
236
| 189| [ Rotate Array] ( https://leetcode.com/problems/rotate-array/ ) | | Easy|
237
237
| 188| [ Best Time to Buy and Sell Stock IV] ( https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ ) | | Hard|
Original file line number Diff line number Diff line change
1
+ public class Solution {
2
+ public int hammingWeight (int n ) {
3
+ int bits = 0 ;
4
+ int mask = 1 ;
5
+ //32位,循环32次
6
+ for (int i = 0 ; i < 32 ; i ++) {
7
+ //利用 mask 的特性 以及 位元算中的且运算
8
+ if ((n & mask ) != 0 ) {
9
+ bits ++;
10
+ }
11
+ //左移一位
12
+ mask <<= 1 ;
13
+ }
14
+ return bits ;
15
+ }
16
+
17
+ }
You can’t perform that action at this time.
0 commit comments