题目

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?

代码

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

Tips:

核心思想是用异或运算。

1. 异或运算满足结合律、交换律;结合这两条定律。

2. 出现偶数次的数最终都会对冲掉,成为0;最后剩下那个只出现奇数次的数

3. 任何数与0异或都是其本身

=========================================

第二次过这道题,一次AC,考查的是位运算的技巧。

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

【Single Number】cpp的更多相关文章

  1. 【Palindrome Number】cpp

    题目: Determine whether an integer is a palindrome. Do this without extra space. click to show spoiler ...

  2. 【Valid Number】cpp

    题目: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " = ...

  3. LeetCode 【Single Number I II III】

    Given an array of integers, every element appears twice except for one. Find that single one. 思路: 最经 ...

  4. 【Letter Combinations of a Phone Number】cpp

    题目: Given a digit string, return all possible letter combinations that the number could represent. A ...

  5. 【Sort Colors】cpp

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  6. 【WildCard Matching】cpp

    题目: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single charact ...

  7. 【Stirling Number】

    两类Stirling Number的简介与区别(参考自ACdreamer的CSDN) Stirling Number I --- s(n,k):将n个物体排成k个非空循环排列(环)的方法数. 递推式: ...

  8. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  9. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

随机推荐

  1. ubuntu 16.04安装nVidia显卡驱动和cuda/cudnn踩坑过程

    安装深度学习框架需要使用cuda/cudnn(GPU)来加速计算,而安装cuda/cudnn,首先需要安装nvidia的显卡驱动. 我在安装的整个过程中碰到了驱动冲突,循环登录两个问题,以至于最后不得 ...

  2. 使用CSS设置Chrome打印背景色

    以下内容适用于Chrome浏览器 打印背景色可以通过在打印预览页面勾选背景图形实现 如果需要在用户不勾选的情况下依然能够打印背景色,可以通过css实现,如,table隔行设置背景色: .data-ta ...

  3. C#使用Aspose.Words操作word文档

    最近接到个需求,由于客服这边要导出大量有一定规则的word文件,里面的内容希望系统自动填充,例如 这里我使用Aspose.Words.dll这个类库, 1.首先,我们需要创建模板文件,毕竟有规则的东西 ...

  4. spring boot 下 spring security 自定义登录配置与form-login属性详解

    package zhet.sprintBoot; import org.springframework.beans.factory.annotation.Autowired;import org.sp ...

  5. centos6.5_64bit-nginx安装部署

    1.配置防火墙,开启80端口.3306端口 vim /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dpor ...

  6. PHP中MySQL数据库连接,数据读写,修改方法

    MySQL连接大的来说有两种方法,一种是mysqli,另一种是mysql.php为连接MySQL提供了函数库,有mysql和mysqli,mysqli是mysql函数库的扩展,是php5才支持的.当你 ...

  7. 【BZOJ1857】传送带(分治经典:三分套三分)

    点此看题面 大致题意: 一个二维平面上有两条传送带\(AB\)和\(CD\),\(AB\)传送带的移动速度为\(P\),\(CD\)传送带的移动速度为\(Q\),步行速度为\(R\),问你从\(A\) ...

  8. AngularJs学习笔记-服务

    服务 (1)在模块中声明的服务对所有组件可见 (2)在组件中声明的服务对自己本身和其子组件 (3)在组件中声明的服务会覆盖在模块中声明的服务 (4)通过@Injectable()装饰器可以在服务中注入 ...

  9. CUDA:Supercomputing for the Masses (用于大量数据的超级计算)-第八节

    原文链接 第八节:利用CUDA函数库 Rob Farber 是西北太平洋国家实验室(Pacific Northwest National Laboratory)的高级科研人员.他在多个国家级的实验室进 ...

  10. 对象 -----JavaScript

    本文摘要:http://www.liaoxuefeng.com/ JavaScript的对象是一种无序的集合数据类型,它由若干键值对组成. JavaScript的对象用于描述现实世界中的某个对象.例如 ...