ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

477. Total Hamming Distance - LeetCode - Partially Correct

2019-09-25 10:55:07  阅读:237  来源: 互联网

标签:Distance repeat 14 int check abs 477 Correct Math


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有