136. Single Number

Posted Carl_Hugo

tags:

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

解题思路:
一个数与它自身异或等于0,将数组中的所有数字异或可以得到只出现一次的数。

public int singleNumber(int[] nums) 
        int s = nums[0];
        for(int i=1;i<nums.length;i++)
            s = s^nums[i];
        
        return s;
    

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

136. Single Number

136. Single Number

136. Single Number

136. Single Number

LeetCode 136. Single Number

Leetcode-136 Single Number