136. Single Number

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?


public class Solution {
    public int singleNumber(int[] nums) {
        for(int i=1;i<nums.length;i++)
        {
            nums[i]^=nums[i-1];
        }
        return nums[nums.length-1];
    }
}


看大神解題方法中另外還有一種很簡單的方法,是使用java8 storm流的特性 reduce方法解決,這裏就不再敘述了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章