136. Single Number

原创 2016年08月30日 23:10:35

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?

大意:给定一个数组,除了一个元素,其它每个元素都出现了两次,找出这个出现一次的元素。时间复杂度O(n), 空间复杂度O(1).

由于数组中除一个元素外 其他元素都出现了2次,因此出现偶数次的元素异或(即二进制模2加)后为0, 因此所以元素异或后即得到只出现一次的数字!

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int sum=0;
        for(int i=0;i<nums.size();i++)
        {
            sum = sum ^ nums[i];

        }
        return sum;

    }
};

相关文章推荐

<LeetCode OJ> 136 / 137 / 260 Single Number(I / II / III)

260. Single Number III My Submissions Question Total Accepted: 19802 Total Submissions: 47891 Di...

Leetcode No. 136. Single Number

Given an array of integers, every element appears twice except for one. Find that single one.Note: ...

Leetcode-位运算-136. Single Number

正式开始用python开始学习数据结构和算法(必要时使用c/c++实现)。leetcode 136. Single NumberProblem: Given an array of integer...

leetcode算法——136. Single Number(基于JAVA)

Given an array of integers, every element appears twice except for one. Find that single one. Note: ...

LeetCode每日一题——136. Single Number

原题地址:  https://leetcode.com/problems/single-number/ Fizz Buzz 描述: ...

leetcode136:single number I&II 及拓展

Single Number I给定一个数组,除了一个元素出现一次之外,其余的元素都出现两次。找出这个元素分析:只要将所有的元素都异或一次,最后的结果就是这个出现一次数。代码:int singleNum...
  • hjxzb
  • hjxzb
  • 2015年04月24日 10:31
  • 521

136/137/260 Single Number

136 Single Number Given an array of integers, every element appears twice except for one. Find tha...

LeetCode#136. Single Number

题目 Single Number:一个数组中,只有一个元素只出现一次,其余元素都出现两次,找出出现一次的这个元素...

LeetCode 136. Single Number 解题报告

LeetCode 136. Single Number 解题报告

136. Single Number

Given an array of integers, every element appears twice except for one. Find that single one.Note: ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:136. Single Number
举报原因:
原因补充:

(最多只允许输入30个字)