Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5e98d7f

Browse files
authored
Merge pull requestneetcode-gh#170 from r1cky0/patch-16
Create 191-Number-of-1-Bits.java
2 parentsd698d9b +3316c2c commit5e98d7f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

‎java/191-Number-of-1-Bits.java‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//standard way
2+
publicclassSolution {
3+
// you need to treat n as an unsigned value
4+
publicinthammingWeight(intn) {
5+
intcount =0;
6+
intmask =1;
7+
for (inti =0;i <32;i++) {
8+
if ((n &mask) !=0) {
9+
count++;
10+
}
11+
n >>=1;
12+
}
13+
returncount;
14+
}
15+
}
16+
17+
//smart way
18+
publicclassSolution {
19+
// you need to treat n as an unsigned value
20+
publicinthammingWeight(intn) {
21+
intcount =0;
22+
while (n !=0) {
23+
n =n & (n -1);
24+
count++;
25+
}
26+
returncount;
27+
}
28+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp