LeetCode 136. Single Number

Posted 一片叶子啊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 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?

 

分析:

使用^即可,最后剩余的结果就是我们需要的。

/**
 * @param {number[]} nums
 * @return {number}
 */
var singleNumber = function(nums) {
    let result = 0;
    for(let i = 0; i <nums.length; i++) {
        result^= nums[i];
    }
    return result;
};

 


以上是关于LeetCode 136. Single Number的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode-136 Single Number

LeetCode 136 Single Number

LeetCode 136. Single Number

LeetCode 136. Single Number

[leetcode]136.Single Number

[Leetcode] 136. Single Number