Skip to content

Commit 22a0507

Browse files
add java solution for leetcode 191
1 parent 3058760 commit 22a0507

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ LeetCode
231231
|200|[Number of Islands](https://leetcode.com/problems/number-of-islands/)| |Medium|
232232
|199|[Binary Tree Right Side View](https://leetcode.com/problems/binary-tree-right-side-view/)| |Medium|
233233
|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|
235235
|190|[Reverse Bits](https://leetcode.com/problems/reverse-bits/)| |Easy|
236236
|189|[Rotate Array](https://leetcode.com/problems/rotate-array/)| |Easy|
237237
|188|[Best Time to Buy and Sell Stock IV](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/)| |Hard|
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}

0 commit comments

Comments
 (0)