2014年4月27日 星期日

[LeetCode] Single Number

Problem:
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?
Solution:O(n)
public class Solution {
    public int singleNumber(int[] A) {
        int ret = 0;
        for(int a:A)
            ret^=a;
        return ret;    
    }
}

沒有留言:

張貼留言