题目要求

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?

一个int数组中,除了有一个int只出现了一次,其他int都出现了两次,请找出这个int。

要求:设计的算法是线性的复杂度,并且不要用额外的内存空间。

解题思路

异或运算的几个相关公式:

1. a ^ a = 0
2. a ^ b = b ^ a
3. a ^ b ^ c = a ^ (b ^ c) = (a ^ b) ^ c
4. d = a ^ b ^ c 可以推出 a = d ^ b ^ c
5. a ^ b ^ a = b
 
本题可以抽象成:int数组里有x1, x2 ... xn(每个出现2次),和y(只出现一次),得出y的值。
由公式2可知,数组里面所有数异或的结果等于 x1^x1^x2^x2^...^xn^xn^y
由公式3可知,上式等于(x1^x1)^(x2^x2)^...^(xn^xn)^y
由公式1可知,上式等于(0)^(0)^...(0)^y = y
 
因此只需要将所有数字异或,就可得到结果。

Java代码

public static int singleNumber(int[] A) {
for (int i = 1; i < A.length; i++) {
A[i] ^= A[i-1];
}
return A[A.length-1];
}

[算法][LeetCode]Single Number——异或运算的巧妙运用的更多相关文章

  1. [LeetCode] Single Number II 位运算

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

  2. leetcode Single Number II - 位运算处理数组中的数

    题目描述: 给定一个包含n个整数的数组,除了一个数出现一次外所有的整数均出现三次,找出这个只出现一次的整数. 题目来源: http://oj.leetcode.com/problems/single- ...

  3. [LeetCode]Single Number 异或的妙用

    1.数组中仅仅有一个元素仅仅出现一次,其余出现偶数次. 利用异或中同样元素相互抵消的方式求解. 2.数组中仅仅有2个元素仅仅出现一次.其余出现偶数次. class Solution { public: ...

  4. LeetCode算法题-Single Number(Java实现)

    这是悦乐书的第175次更新,第177篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第34题(顺位题号是136).给定一个非空的整数数组,除了一个元素外,每个元素都会出现两 ...

  5. leetcode 268 Missing Number(异或运算的应用)

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  6. [LeetCode] Single Number 单独的数字

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

  7. LeetCode Single Number III

    原题链接在这里:https://leetcode.com/problems/single-number-iii/ 题目: Given an array of numbers nums, in whic ...

  8. [LeetCode] Single Number III 单独的数字之三

    Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...

  9. [LeetCode] Single Number II 单独的数字之二

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

随机推荐

  1. JVM性能调优监控工具jps、jstack、jmap、jhat、jstat、hprof

    JVM性能调优监控工具jps.jstack.jmap.jhat.jstat.hprof

  2. nexus 配置

    1.下载 http://www.sonatype.org/nexus/go/  例如:nexus-2.11.4-01-bundle.tar.gz 2.解压 tar -xzvf nexus-2.11.4 ...

  3. atitit。win7 win8 win9 win10 win11 新特性总结与战略规划

    atitit.win7 win8 win9 win10  win11 新特性总结与战略规划 1. win7 1 1.1. 发布时间 2009年10月22日 1 1.2. 稳定性大幅提升,很少蓝屏死机 ...

  4. Atitit . 编程模型的变革总结

    Atitit . 编程模型的变革总结 1. 面向对象与面向过程程序设计有如下不同:  1 1.1. 函数与数据是否分离.... 1 1.2. 以功能为中心;以数据为中心..... 1 1.3. 事件驱 ...

  5. CHero

    #ifndef __HERO_H__ #define __HERO_H__ #include "GameFrameHead.h" #include "GameParam. ...

  6. Struts2对AJAX的支持

    一.简介        struts2确实一个非常棒的MVC框架.这里部分记述一下struts2对AJAX的支持.实现AJAX有两种方式,一种是使用原生的javascript代码实现,一种是使用第三方 ...

  7. iOS状态栏标志

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; //显示 [UIApplication sharedA ...

  8. poj2082单调栈

    本来实在做后缀数组的题目的,不巧,碰到了pku3415这题,需要用到单调栈来维护,但是之前又没有学习过单调栈这方面的知识,于是水了几题....... 题意:给你一些连续的小矩形,高宽不定,求最大的矩形 ...

  9. CentOS minimal 版安装图形界面的步骤分享,中文语言包

    1.连接网络: CentOS minimal.iso安装好后,进入终端,默认是不开网络的, 首先启用网卡, 自动获取ip. ifconfig eth0 up dhclient eth0 这时候再 if ...

  10. Spring--初始化IOC容器的几种方式

    初始化beanfactory主要有以下的三种方式:    1.filesystemXml Resource resource = new FileSystemResource("beans. ...