翻译

给定一个整型数组,除了某个元素外其余元素均出现两次。

找出这个仅仅出现一次的元素。

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

原文

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?

分析

请參照上一题:LeetCode 260 Single Number III(仅仅出现一次的数字3)(*)

另一道与之相应的题:LeetCode 137 Single Number II(仅仅出现一次的数字 II)(*)

代码

class Solution {
public:
unsigned int FindFirstBigIs1(int num) {
int indexBit = 0;
while (((num & 1) == 0) && (indexBit < 8 * sizeof(int))) {
num = num >> 1;
++indexBit;
}
return indexBit;
} int singleNumber(vector<int>& nums) {
if (nums.size() <= 0) return NULL; int resultExclusiveOR = 0;
for (int i = 0; i < nums.size(); ++i)
resultExclusiveOR ^= nums[i]; unsigned int indexOf1 = FindFirstBigIs1(resultExclusiveOR); int singleNum = 0;
for (int j = 0; j < nums.size(); ++j) {
singleNum ^= nums[j];
}
return singleNum;
}
};

LeetCode 136 Single Number(仅仅出现一次的数字)的更多相关文章

  1. LeetCode 136. Single Number(只出现一次的数字)

    LeetCode 136. Single Number(只出现一次的数字)

  2. Leetcode 136 Single Number 仅出现一次的数字

    原题地址https://leetcode.com/problems/single-number/ 题目描述Given an array of integers, every element appea ...

  3. leetcode 136 Single Number, 260 Single Number III

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

  4. LeetCode 136. Single Number C++ 结题报告

    136. Single Number -- Easy 解答 相同的数,XOR 等于 0,所以,将所有的数字 XOR 就可以得到只出现一次的数 class Solution { public: int ...

  5. leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)

    136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...

  6. [LeetCode] 136. Single Number 单独数

    Given a non-empty array of integers, every element appears twice except for one. Find that single on ...

  7. LeetCode 136. Single Number (落单的数)

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

  8. LeetCode - 136. Single Number - ( C++ ) - 解题报告 - 位运算思路 xor

    1.题目大意 Given an array of integers, every element appears twice except for one. Find that single one. ...

  9. leetcode 136 Single Number bBt Option

    Linked Url:https://leetcode.com/problems/single-number/ Given a non-empty array of integers, every e ...

随机推荐

  1. Git 如何把master的内容更新到分支

    Background: 当有人对master进行更新之后,你想让已经创建的分支内容更新到master的最新状态, bpan@5CG7022BM2 MINGW64 /d/GitRep/JIRA_Exte ...

  2. OpenJDK源码研究笔记(九)-可恨却又可亲的的异常(NullPointerException)

    可恨的异常 程序开发过程中,最讨厌异常了. 异常代表着程序出了问题,一旦出现,控制台会出现一屏又一屏的堆栈错误信息. 看着就让人心烦. 对于一个新人来讲,遇到异常经常会压力大,手忙脚乱,心生畏惧. 可 ...

  3. 【Henu ACM Round#19 D】 Points on Line

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 考虑l..r这个区间. 且r是满足a[r]-a[l]<=d的最大的r 如果是第一个找到的区间,则直接累加C(r-l+1,3); ...

  4. 【SRM 716 DIV 1 A】 ConstructLCS

    Problem Statement A string S is a subsequence of a string T if we can obtain S from T by erasing som ...

  5. spring AOP的Pointcut注解报错

    error at ::0 can't find referenced pointcut spring使用的是4.1.0,在项目中直接复制旧的aspectjweave.jar报错了 然后换成aspect ...

  6. Linux Virtual Server技术

    1 LVS简单介绍 Linux VirtualServer是一个高扩展和高可用性server,在一个真正server的集群中构建而成,包括Linux操作系统中的负载均衡. server的架构对于终端用 ...

  7. [React] Implement a React Context Provider

    If you have state that needs to exist throughout your application, then you may find yourself passin ...

  8. 转:Mac下搭建svn服务器和XCode配置svn

    Mac下搭建svn服务器和XCode配置svn 先打开命令行终端. 1.创建svn repository svnadmin create /yourpath/svnroot/repository 2. ...

  9. Invalid command &#39;WSGIScriptAlias&#39;, perhaps misspelled or defined by a module not included in the ser

    没有Include wsgi,执行: sudo a2enmod wsgi 可能出现以下的错误 ERROR: Module mod-wsgi does not exist! 安装 libapache2- ...

  10. HIT 2255 Not Fibonacci(矩阵高速幂)

    #include <iostream> #include <cstdio> #include <cstring> using namespace std; cons ...