5/30/2014

01. Single Number

public class Solution {
    public int singleNumber(int[] A) {
        int res = A[0];
     
        if (A.length==1) return res;
     
        for (int i=1; i<A.length; i++) {
            res = res ^ A[i]; //XOR
        }
     
        return res;
    }
}

1 条评论:

  1. 1. O(n) time --> no sort
    2. O(1) space --> no hash map

    So, XOR.

    回复删除