2014年5月6日星期二

Bit Solution - Single Number - array

The tricky part is that this problem asks to have sense of bit operators… & || ^

public class Solution {
    public int singleNumber(int[] A) {
        if(A == null || A.length == 0){
            return Integer.MIN_VALUE;
        }
    /*  
        Set<Integer> process = new TreeSet<Integer>();
        for(int i = 0; i < A.length; i++){
            if(!process.add(A[i])){
                process.remove(A[i]);
            }
        }
        Object[] res = process.toArray();
        return (Integer)res[0];
    */
       
        int result = A[0];
        for(int i = 1; i < A.length; i++){
            result = result ^ A[i];
        }
        return result;
       
    }
}

没有评论: