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

Commitc329092

Browse files
committed
Added Number of 1 Bits
1 parent68d8191 commitc329092

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
classSolution {
2+
public:
3+
inthammingWeight(uint32_t n) {
4+
int ans =0;
5+
6+
while (n !=0) {
7+
ans++;
8+
n &= (n -1);
9+
}
10+
11+
return ans;
12+
}
13+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
publicclassSolution {
2+
publicinthammingWeight(intn) {
3+
intans =0;
4+
5+
while (n !=0) {
6+
ans++;
7+
n &= (n -1);
8+
}
9+
10+
returnans;
11+
}
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
varhammingWeight=function(n){
2+
varans=0;
3+
4+
while(n!==0){
5+
ans++;
6+
n&=(n-1);
7+
}
8+
9+
returnans;
10+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
classSolution:
2+
defhammingWeight(self,n:int)->int:
3+
ans=0
4+
5+
whilen!=0:
6+
ans+=1
7+
n=n& (n-1)
8+
9+
returnans
10+
# Time: O(Bits), Space: O(1)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp