FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode/src/main/java/L0477_TotalHammingDistance.java at master · LjyYano/LeetCode · GitHub
LjyYano
/
LeetCode
Public
Notifications
You must be signed in to change notification settings
Fork
125
Star
342
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCode
/
src
/
main
/
java
/
L0477_TotalHammingDistance.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
19 lines (17 loc) · 543 Bytes
Breadcrumbs
LeetCode
/
src
/
main
/
java
/
L0477_TotalHammingDistance.java
Copy path
File metadata and controls
19 lines (17 loc) · 543 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public
class
L0477_TotalHammingDistance
{
public
int
totalHammingDistance
(
int
[]
nums
) {
int
n
=
nums
.
length
;
int
ans
=
0
;
// 遍历 32 位
for
(
int
i
=
0
;
i
<
32
;
i
++) {
int
count1
=
0
;
// 统计第 i 位上有多少个 1
for
(
int
num
:
nums
) {
count1
+= (
num
>>
i
) &
1
;
}
// 第 i 位的贡献:count1 个 1 和 (n - count1) 个 0
ans
+=
count1
* (
n
-
count1
);
}
return
ans
;
}
}
Back
|
FazBrowse Home
|
New Git URL