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. 6/7 Sprint2 看板和燃尽图

  2. ubuntu14.04LS中安装sogouPingyin

    简介: 安装了新版本的ubuntu我才发现itbus的拼音是双拼的  没法改变 所以就安装了搜狗拼音 , 好像这个和小企鹅输入法有点关系 首先: 在终端中输入: sudo add-apt-reposi ...

  3. JS实现漂亮的窗口拖拽效果(可改变大小、最大化、最小化、关闭)

    转自<JS实现漂亮的窗口拖拽效果(可改变大小.最大化.最小化.关闭)>:http://www.jb51.net/article/73157.htm   这篇文章主要介绍了JS实现漂亮的窗口 ...

  4. BOM组件物料重复检查

    好吧,今天同事让做个BOM组件物料重复检查 网上有很多例子都是在保存的时候检查的,用的是BADI :BOM_UPDATE 自己也试了一下,麻烦....很麻烦...尤其是在重复检查的时候: METHOD ...

  5. oracle用户

    如果要了解oracle中用户信息,可以查询数据字典dba_users.在sql*plus中,使用system用户登录,查询语句如下: select username,account_status fr ...

  6. Oracle11g中ORA-01790

    问题源于群里有人问如何让查询的结果值+1,方法其实很简单,直接在SQL语句中+1就可以,如果有空可以用NVL处理. 但是测试的时候我使用了UNION ALL(测试的字段是varchar2类型),结果报 ...

  7. ANGULARJS 出错解决

    先上代码 程序的意思是使用eval更改指令父scope的数据,在调用$digest 这样就出错了,出错信息如下: 解决办法: 增加timeout事件,具体原因还不是很清楚,反正问题目前是解决了.

  8. 使用Join代替In

    我们知道,在sql中使用IN让我们的where子句可以规定多个值.当需要从一个集合中查询包含某几个值的记录的时候,通常我们会选择使用IN来实现,其实,使用JOIN也可以实现这样的功能,而且性能要比IN ...

  9. 关于linux服务器的批量维护、批量升级、

    由于版权费用原因,众多中小服务器采用linux服务器进行功能处理.在进行批量升级.更新业务程序时.可以采用如下批量方案: 1.打包成rpm包,并配置yum源服务器,以支持个人的yum服务器进行处理.一 ...

  10. Ibatis.Net 入门示例(一)

    新项目用的是Ibatis.Net.哥的感觉是NHibernate才是哥的最爱.没办法,即使重复,也要抽时间学习.努力做好这份工作再说吧. 一.Ibatis.Net-SQLServer2008 还是以哥 ...