Single Number

https://leetcode.com/problems/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?

 1 public class Solution {
 2     public int singleNumber(int[] nums) {
 3         int ans=0;
 4         int len=nums.length;
 5         for(int i=0;i<len;i++){
 6             ans^=nums[i];
 7         }
 8         return ans;
 9     }
10 }

 

posted @ 2015-05-02 11:37  打小孩  阅读(98)  评论(0编辑  收藏  举报