GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Leetcode-Java/leetcode/solution/src/TotalHammingDistance.java at master · Tulpen/Leetcode-Java

Expand file tree

/

TotalHammingDistance.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
23 lines (23 loc) · 645 Bytes
/

TotalHammingDistance.java

Copy path

File metadata and controls

23 lines (23 loc) · 645 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
20
21
22
23
public class TotalHammingDistance {
/**
* 思路是依次遍历每个数字的第i位,统计出为1的有m个,为0的有n个,则在这一位上
* 增加的hamming distance为m*n个
*/
/**
* 复杂度为O(n)
*/
public int totalHammingDistance(int[] nums) {
int res = 0;
for (int i = 0; i < 32; i++) {
int a = 0;
for (int j = 0; j < nums.length; j++) {
if ((nums[j] & 1) != 0) {
a++;
}
nums[j] >>>= 1;
}
res += a * (nums.length - a);
}
return res;
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.