Prolbem link:

http://oj.leetcode.com/problems/single-number/

This prolbem can be solved by using XOR(exclusive OR) operation. Let x, y, and z be any numbers, and XOR has following properties:

  1. x XOR 0 is itself, i.e. x XOR 0 = 0;
  2. x XOR x is 0, i.e. x XOR x = 0;
  3. XOR is associative, i.e. (x XOR y) XOR z = x XOR (y XOR z).

So for list contains following (2m+1) numbers:

x0, x1, x1, x2, x2, ..., xm, xm.

If we XOR them together, according to the three properties above, we would obtain the result x0, which is just the single number the problem asks.

The following is my python solution accepted by oj.leetcode.

class Solution:
# @param A, a list of integer
# @return an integer
def singleNumber(self, A):
"""
We use exclusive OR operation for this problem.
XOR has following two properties:
1) Zero-Self. x XOR 0 = x for any number x
2) Self-Zero. x XOR x = 0 for any number x
3) XOR is associative. (x XOR y) XOR z = x XOR (y XOR z)
Suppose A is consisiting of n numbers,
x0, x1, x2, ..., xm, x1, x2, ..., xm (n = 2m + 1)
Then we XOR them tegother, and by the associative property,
we have the result as
res = x0 XOR (x1 XOR x1) XOR (x2 XOR x2) XOR ... XOR (xm XOR xm).
By Self-Zero property, res = x0 XOR 0 XOR ... XOR 0.
By Zero-Self property, res = x0, it is just the single integer!
"""
assert len(A)>0
res = 0
for n in A:
res = res ^ n
return res

  

【LEETCODE OJ】Single Number的更多相关文章

  1. 【LEETCODE OJ】Single Number II

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

  2. 【Leetcode 136】Single Number

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

  3. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  4. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  5. 【LeetCode】【Python题解】Single Number &amp; Maximum Depth of Binary Tree

    今天做了三道LeetCode上的简单题目,每道题都是用c++和Python两种语言写的.由于c++版的代码网上比較多.所以就仅仅分享一下Python的代码吧,刚学完Python的基本的语法,做做Lee ...

  6. 【LeetCode OJ】Maximum Depth of Binary Tree

    Problem Link: https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ Simply BFS from root an ...

  7. 【LeetCode OJ】Triangle

    Problem Link: http://oj.leetcode.com/problems/triangle/ Let R[][] be a 2D array where R[i][j] (j < ...

  8. 【LeetCode OJ】Best Time to Buy and Sell Stock III

    Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ Linear Time Solut ...

  9. 【LeetCode OJ】Word Ladder I

    Problem Link: http://oj.leetcode.com/problems/word-ladder/ Two typical techniques are inspected in t ...

随机推荐

  1. Spring Data Jpa配置

    Spring Data JPA提供的接口,也是Spring Data JPA的核心概念: 1:Repository:最顶层的接口,是一个空的接口,目的是为了统一所有Repository的类型,且能让组 ...

  2. jquery添加的html元素按钮为什么不执行类样式绑定的click事件

    代码举例: 更多按钮: <input type="button" class="addMore" id="addMore${issue.id } ...

  3. 20145236 《Java程序设计》 第6周学习总结

    20145236 <Java程序设计>第6周学习总结 教材学习内容总结 第十章 输入/输出 InputStream与OutputStream 串流设计的概念 Java将输入/输出抽象化为串 ...

  4. AP聚类算法(Affinity propagation Clustering Algorithm )

    AP聚类算法是基于数据点间的"信息传递"的一种聚类算法.与k-均值算法或k中心点算法不同,AP算法不需要在运行算法之前确定聚类的个数.AP算法寻找的"examplars& ...

  5. FZU 2029 买票问题 树状数组+STL

    题目链接:买票问题 思路:优先队列维护忍耐度最低的人在队首,leave操作ok. vis数组记录从1到n的编号的人们是不是在队列中,top维护队首的人的编号.pop操作搞定. 然后,check操作就是 ...

  6. mysql创建PATH快捷

    1.使其临时生效 PATH=$PATH:/usr/local/mysql/bin 2.永久生效 编辑/etc/profile  添加第79列 然后source /etc/profile 3.输入命令m ...

  7. Net开发环境配置

    Web开发插件: 1.JSEnhancements js和css折叠插件 可以参见dudu的介绍不错的VS2010扩展——JSEnhancements,让js和css也折叠 下载地址:http://v ...

  8. java面向对象编程--第九章 多态和抽象

    9.1多态 在生物学中,是指一个生物或物种可以有多种不同的形式或阶段: 在OOP中,多态是指一个对象有多种形式的能力. 多态可以说是面向对象编程的精髓所在.java中之所以引入多态的概念,原因之一是它 ...

  9. apache日志轮转

    apache默认的日志配置为:     ErrorLog "logs/error_log"     CustomLog "logs/access_log" co ...

  10. Hduacm—5497

    #include <cstring> #include <cstdio> #include <iostream> using namespace std; type ...