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. PHP7.1新特性一览

    PHP7.0的性能是PHP5.6的两倍 http://www.phpchina.com/article-40237-1.html 可空类型 list 的方括号简写 void 返回类型 类常量属性设定 ...

  2. Python Numpy Array

    Numpy 是Python中数据科学中的核心组件,它给我们提供了多维度高性能数组对象. Arrays Numpy.array   dtype 变量 dtype变量,用来存放数据类型, 创建数组时可以同 ...

  3. 使用nrm解决npm下载包慢的问题!

    nrm的安装使用 作用:提供了一些最常用的NPM包镜像地址,能够让我们快速的切换安装包时候的服务器地址: 什么是镜像:原来包刚一开始是只存在于国外的NPM服务器,但是由于网络原因,经常访问不到,这时候 ...

  4. java生成复杂word文档

    在Web应用中,有时需要按照固定的模板将数据导出到Word,如流程审批单,在流程处理完成后将处理过程按照流程单的要求导出,有时程序中需要实现生成 标准Word文档,要求能够打印,并且保持页面样式不变, ...

  5. JavaSE---反射(未完待续)

    1.概述 1.1 Java程序中许多对象在运行时会出现2种类型:编译时类型.运行时类型: eg:Person  person=new Student(); 这行代码在编译时为Person类型,运行时为 ...

  6. matlab 图像和 opencv 图像的相互转换

    matlab可以生成C++代码, 但是在涉及图像数据的时候,要注意数据格式的转换. 1. Matlab图像数据在内存中的存放顺序是R通道图,G通道图,B通道图.对于每个通道,数据存放是先列后行. 2. ...

  7. (转)DNS原理及其解析过程

    DNS原理及其解析过程原文:http://blog.51cto.com/369369/812889 网络通讯大部分是基于TCP/IP的,而TCP/IP是基于IP地址的,所以计算机在网络上进行通讯时只能 ...

  8. 虚拟机扩容(/dev/mapper/centos-root 空间不足)

    1:.首先查看我们的根分区大小是多少 df -h 文件系统                类型      容量  已用  可用 已用% 挂载点 /dev/mapper/centos-root xfs  ...

  9. 关于Mysql数据库的注意点

    1.注意属性为String的数据在JDBC操作语句中要加单引号 例子: conn = DriverManager.getConnection("jdbc:mysql://localhost: ...

  10. HDU 3829——Cat VS Dog——————【最大独立集】

    Cat VS Dog Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit S ...