Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Problem: https://leetcode.com/problems/total-hamming-distance/
- // Solution: https://leetcode.com/problems/total-hamming-distance/discuss/247217/Java-Solutions
- class Solution {
- public int totalHammingDistance(int[] nums) {
- int total = 0;
- for(int i = 0; i < 32; i++) {
- int countOnes = 0;
- int countZeroes = 0;
- for (int x: nums) {
- if((x >> i & 1) == 1) {
- countOnes ++;
- } else {
- countZeroes ++;
- }
- }
- total += countZeroes * countOnes;
- }
- return total;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement