Given a non-empty 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?

Example 1:

Input: [2,2,1]
Output: 1

Example 2:

Input: [4,1,2,1,2]
Output: 4

两个相同的数异或为0,一个数和0异或不变,因此将所有的数异或即只出现一次的数。

 class Solution {
public:
int singleNumber(vector<int>& nums) {
int ans = ;
for (int i : nums) {
ans ^= i;
}
return ans;
}
};

leetcode 136、Single Number的更多相关文章

  1. [LeetCode#136, 137]Single Number, Single Number 2

    The question: Single Number Given an array of integers, every element appears twice except for one. ...

  2. leetcode@ [136/137] Single Number & Single Number II

    https://leetcode.com/problems/single-number/ Given an array of integers, every element appears twice ...

  3. 【Leetcode 136】Single Number

    问题描述:给出一个整数数组,除了一个元素外,其他每个元素都出现了2次,找出只出现1次的元素. int singleNumber(vector<int>& nums); 分析:比较自 ...

  4. leetcode 136 137 Single Number

    题目描述(面试常考题) 借助了异或的思想 class Solution { public: int singleNumber(vector<int>& nums) { ; ; i ...

  5. 【一天一道LeetCode】#260. Single Number III

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  7. LeetCode 笔记26 Single Number II

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  8. 【LEETCODE OJ】Single Number II

    Problem link: http://oj.leetcode.com/problems/single-number-ii/ The problem seems like the Single Nu ...

  9. 【LEETCODE OJ】Single Number

    Prolbem link: http://oj.leetcode.com/problems/single-number/ This prolbem can be solved by using XOR ...

随机推荐

  1. java的Spring学习2--构造函数注入

    bean文件如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi=" ...

  2. 设置input的样式

    css中的 ” 七层重叠法 ” :即网页内容先后顺序分别为:背景边框,负值z-index,display:block,浮动,display:inline-block,z-index:auto,正值z- ...

  3. 51Nod - 1242 斐波那契(快速幂)

    斐波那契数列的定义如下:   F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2)   (1, 1, 2, 3, 5, 8, 13, 21, ...

  4. Linux进程控制理论及几种常见进程间通信机制

    1. Linux进程控制理论 ① 进程是一个具有一定独立功能的程序的一次运行活动(动态性.并发性.独立性.异步性). 进程的四要素: (1)有一段程序供其执行(不一定是一个进程所专有的),就像一场戏必 ...

  5. log4net独立配置文件配置(winfrom)

    log4net配置很多,具体配置步骤不细说,具体说出个人遇到的问题. 在winfrom和web应用程序中配置,在默认配置文件配置都没问题,因为EF也写在默认配置文件中,就会冲突解决办法就是将log4. ...

  6. Hive 报错信息及解决方法

    return code 2 为SQL报错. return code 1 一般为权限问题. 具体要看源码.

  7. java——删除HashMap中所有的键值对

    第一种:❌(报错) import java.util.HashMap; import java.util.Set; public class T{ public static void main(St ...

  8. VMware 无法连接虚拟设备ide1:0,主机上没有相对应的设备,您 要在每次开启此虚拟机时都尝试连接此虚拟设备吗?

    无法连接虚拟设备ide1:0,主机上没有相对应的设备,您 要在每次开启此虚拟机时都尝试连接此虚拟设备吗? 运行VMware,出现以上错误. ide1:0一般是虚拟机的光驱,配置默认选项是“使用物理驱动 ...

  9. 转 [Error]EOL while scanning string literal

    https://blog.csdn.net/orangleliu/article/details/38943749 项目中有个定时任务,每天取到一些表数据传到一个外部接口,但是最近总是有异常,今天查了 ...

  10. swagger demo code

    //Application 开启注解 @EnableSwagger2public class Application { public static void main(String[] args) ...