美文网首页LeetCode
[LeetCode] 136. 只出现一次的数字

[LeetCode] 136. 只出现一次的数字

作者: 拉面小鱼丸 | 来源:发表于2018-04-10 14:20 被阅读0次

给定一个整数数组,除了某个元素外其余元素均出现两次。请找出这个只出现一次的元素。

备注:

你的算法应该是一个线性时间复杂度。 你可以不用额外空间来实现它吗?

英文

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?

JAVA

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

相关文章

网友评论

    本文标题:[LeetCode] 136. 只出现一次的数字

    本文链接:https://www.haomeiwen.com/subject/jsxnhftx.html