朝花夕拾

Clicks&Changes

导航

Single Number

  Given an array of integers, every element appears twice except for one. Find that single one.Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

  线性时间完成,不花费多余空间,问题特殊,异或运算就解决了。

public class Solution {
    public int singleNumber(int[] nums) {
        int result = 0;
        for(int num: nums)
        {
            result^=num;
        }
        return result;
    }
}

 

posted on 2015-10-06 14:03  Draug  阅读(98)  评论(0)    收藏  举报