Algorithm problem - only occurs once digital

In addition to an array of integers in two numbers, the other numbers appear twice. Please write program to find these two numbers appear only once

// num1, num2 respectively array of length 1. Outgoing parameters
 // will num1 [0], num2 [0 ] is set to return results 
public  class Solution {
     public  static  void FindNumsAppearOnce ( int [] Array, int num1 [], int num2 []) {
         IF (Array == null be array.length || <=. 1 ) {
            num1[0] = num2[0] = 0;
            return;
        }
        int len = array.length, index = 0, sum = 0;
        for(int i = 0; i < len; i++){
            sum ^= array[i];
        }
        for(index = 0; index < 32; index++){
            if((sum & (1 << index)) != 0) break;
        }
        for(int i = 0; i < len; i++){
            if((array[i] & (1 << index))!=0){
                num2[0] ^= array[i];
            }else{
                num1[0] ^= array[i];
            }
        }
    }
/**
     * Only one of an array of a number appears once, have emerged in several other twice this number to find out
     * @Param the
     * @return
     */
    public static int find1From2(int[] a){
        int len = a.length, res = 0;
        for(int i = 0; i < len; i++){
            res = res ^ a[i];
        }
        return res;
    }
/**
     * Only one of an array of a number appears once, other figures have appeared three times to find this number
     * @Param the
     * @return
     */
    public static int find1From3(int[] a){
        int[] bits = new int[32];
        int len = a.length;
        for(int i = 0; i < len; i++){
            for(int j = 0; j < 32; j++){
                bits[j] = bits[j] + ( (a[i]>>>j) & 1);
            }
        }
        int res = 0;
        for(int i = 0; i < 32; i++){
            if(bits[i] % 3 !=0){
                res = res | (1 << i);
            }
        }
        return res;
    }
}

 

Guess you like

Origin www.cnblogs.com/mxj961116/p/11918608.html