[leetcode]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?

此题如果不存在线性时间解决这一条件的话很容易解决(不管哪种方式下N方一定可以出结果)。最后是敖神的教导,附上代码:

public class Solution {
    public int singleNumber(int[] nums) {
        int index, res;
        res = nums[0];
        if(nums.length == 1)    return res;
        else{
            for(index = 1; index < nums.length; index++){
                res = res ^ nums[index];
            }
            return res;
        }
    }
}

的确是脑洞大开,很精彩的方法,mark一下。

题目链接:https://leetcode.com/problems/single-number/

你可能感兴趣的:(LeetCode,算法)