[leetcode]single number

2016-07-27  本文已影响11人  这是朕的江山

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?

解题思路:使用异或,数组中相同的数两两异或全部为0,最后剩下的就是那个落单的数。

public class Solution {
    public int singleNumber(int[] A) {
        int n=0;
        for(int i=0;i<A.length;i++)
        {
            n=n^A[i];
        }
        return n;
    }
}
上一篇下一篇

猜你喜欢

热点阅读