Happiness is more than pleasure without pain

你只有非常努力,才能看起来毫不费力

导航

SingleNumber

Given an array of integers, every element appears twice except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

//意思是一堆数中找出没成对的那个,用异或效率最高A^0=A   A^A=0 最后可求出SingleNumber

public class Solution {

    public int singleNumber(int[] A) {
        int res=0;
        for(int i=0;i<A.length;i++){
            res^=A[i];
        }
        return res;
    }
}

posted on 2014-09-16 22:59  believer  阅读(90)  评论(0编辑  收藏  举报