其他分享
首页 > 其他分享> > 477. Total Hamming Distance - LeetCode - Partially Correct

477. Total Hamming Distance - LeetCode - Partially Correct

作者:互联网

Description:

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Now your job is to find the total Hamming distance between all pairs of the given numbers.

Example:

Input: 4, 14, 2

Output: 6

Explanation: In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just
showing the four bits relevant in this case). So the answer will be:
HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.

 

Note:

  1. Elements of the given array are in the range of 0 to 10^9
  2. Length of the array will not exceed 10^4.

 

Accepted 52,031 Submissions 104,845

Solution:

Attempt 1 time limit Exceeded. Most cases passed ..

But need to be optimized.

 

 

 

class Solution {
    public int totalHammingDistance(int[] nums) {
    
        int sum = 0; 
        
        for(int i = 0; i<nums.length; i++){
            
            for(int j = i+1; j<nums.length; j++){
               // System.out.println(nums[i]+" "+nums[j]);
                 sum = sum +GetHammingDistance(nums[i],nums[j]);
            }
        }
        
      
        
       //System.out.println( GetHammingDistance(4,2));
        
        return sum;
    }
    
    static int GetHammingDistance(int num_a, int num_b){
        
        String a_s = binary(num_a);
        String b_s = binary(num_b);
        
        int a = a_s.length();
        int b = b_s.length();
   
        int smaller = a>b? b: a;
        int count = 0; 
        
        String check_a = a_s;
        String check_b = b_s;
        
        if(a ==smaller){
            
            check_a = repeat(Math.abs(a-b))+a_s;
            //System.out.println( (repeat(Math.abs(a-b))+a_s));   
        }
        else{
            check_b = repeat(Math.abs(a-b))+b_s;
            //System.out.println( (repeat(Math.abs(a-b))+b_s));   
        }
       
        for(int i = 0; i<check_a.length(); i++){
            
            if(check_a.charAt(i)!=check_b.charAt(i)){
                //System.out.println("Check");
                count++;
            }
        }
        
        //System.out.println(check_a);
        //System.out.println(check_b);
        
        return count;
    }
    
    static String repeat(int a){
        String res = "";
        
        for(int i = 1; i<=a;i++){
            
            res = res+"0";
        }
        
        return res;
    }
    static String binary(int num){
        
        StringBuffer res = new StringBuffer();
        
        while(num>0){
            
            int dig = num%2;
            
            res.append(dig);
            
            num = num/2;
            
        }
        
        return res.reverse().toString();
    }
}

 

 

标签:Distance,repeat,14,int,check,abs,477,Correct,Math
来源: https://www.cnblogs.com/codingyangmao/p/11583255.html