题目

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. [转]ubuntu16.04安装teamviewer12依赖包解决

    安装teamviewer下载地址:http://www.teamviewer.com/en/download/linux/ 下载的是:teamviewer_12.0.76279_i386.deb   ...

  2. 在wpf或winform关闭子窗口或对子窗口进行某个操作后刷新父窗口

    父窗口: ///<summary> ///弹出窗口  ///</summary> ///<param name="sender"></pa ...

  3. mongodb 认证方式(version:3.0.4)

    之前一直在本机上跑,前段时间把后台架到云服务器上了,在settings里加上了username和password,希望同步的时候修改一下settings就能在本地测试.   因为云服务器端数据库连接需 ...

  4. python3爬虫03(find_all用法等)

    #read1.html文件# <html><head><title>The Dormouse's story</title></head># ...

  5. Redis 优缺点

    REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统. Redis是一个开源的使用ANSI C语言编写.遵守B ...

  6. pat甲级1114

    1114 Family Property(25 分) This time, you are supposed to help us collect the data for family-owned ...

  7. pta数据结构编程题

    编程题6 树的同构 编程题7 List Leaves 编程题8 Tree Traversals Again 编程题10 Root of AVL Tree 编程题12 堆中的路径 编程题13 File ...

  8. idea字体模糊

    用jdk1.8的jre替换idea的jre64,但是记得在lib里加上jdk的lib中的tools.jar. 如图: 然后 将原来jre64的TOOLS.jar拷贝到替换后的jre的lib目录下,重启 ...

  9. SOA体系-三大核心部件

    1.ESB(Enterprise Service Bus)企业服务总线.ESB是传统中间件技术与XML.Web服务等技术结合的产物.ESB提供了网络中最基本的连接中枢,是构筑企业神经系统的必要元素.从 ...

  10. 2018.2.5 PHP如何写好一个程序用框架

    随着PHP标准和Composer包管理工具的面世,普通开发者撸一个框架已经不再是什么难事了. 无论是路由管理.ORM管理.还是视图渲染都有许许多多优秀的包可以使用.我们就像堆积木一样把这些包用comp ...