Lintcode算法题:找出数组中不重复的数

题目:

给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字。

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

示例:[1,2,3,2,1,3,4,4,6] 输出6

注意:

只能遍历一次

Note:

我们的算法应该具有线性运行时复杂性。你能在不使用额外内存的情况下实现它吗?
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

解题:

public class Solution {
    public int singleNumber(int[] A) {
        if(A == null || A.length == 0) {
            return -1;
        }
        int rst = 0;
        for (int i = 0; i < A.length; i++) {
            rst ^= A[i];
        }
        return rst;
    }
}

清新脱俗!

posted @ 2019-01-22 22:21  yorkmass  阅读(1179)  评论(1)    收藏  举报