Advertisement
Guest User

477. Total Hamming Distance | Time:O(n^2) | Space: O(1)

a guest
Aug 21st, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.54 KB | None | 0 0
  1. // Problem: https://leetcode.com/problems/total-hamming-distance/
  2. // Solution: https://leetcode.com/problems/total-hamming-distance/discuss/247217/Java-Solutions
  3.  
  4. class Solution {
  5.     public int totalHammingDistance(int[] nums) {
  6.         int total = 0;
  7.        
  8.         for (int i = 0; i < nums.length - 1; i++) {
  9.            
  10.             for(int j = i + 1; j < nums.length; j++) {
  11.                
  12.                 total += Integer.bitCount(nums[i] ^ nums[j]);
  13.                
  14.             }
  15.         }
  16.        
  17.         return total;
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement