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. Linux基础:软件安装(rpm,yum,源代码)

    Software Installation on Linux Linux安装分为rpm包(可通过yum或者是rpm命令安装)和源码包(源代码或者是编译过的二进制码)两种. Linux是开源系统,很多应 ...

  2. 阮一峰:RSA算法原理(一)

    今天看到一篇好文章,关于加密算法,收藏了觉得不过瘾,还是自己贴一遍,也能加深一下印象. 原文链接:http://www.ruanyifeng.com/blog/2013/06/rsa_algorith ...

  3. protobuf 安装 及 小测试

    参考:http://shift-alt-ctrl.iteye.com/blog/2210885 版本: 2.5.0 百度云盘上有jar包. mac 上安装: 新建:/Users/zj/software ...

  4. 解决ACTIVITI流程图设置字体不生效的问题

    在ACTIVITI 5.15的版本中,有一个设置流程图的字体配置. 配置如下: <bean id="processEngineConfiguration" class=&qu ...

  5. Scala 入门——Eclipse开发环境搭建

    Come From: http://lidrema.blog.163.com/blog/static/209702148201461145859142/ Scala: 一种类似java的编程.集成了面 ...

  6. java面向对象编程——第五章 对象的行为

    5.1 方法调用栈 当一个方法被调用时,将导致控制流程跳转到被调用的方法.然后,控制流程执行方法中的语句.当然,被执行的方法可能会调用其它方法,导致控制流程跳转到其它方法.所有的方法调用都维护在一个称 ...

  7. js基础之数组

    数组方法 添加: push arr.push();//尾部添加 unshift arr.unshift();//头部添加 删除: pop arr.pop();//尾部删除 shift arr.shif ...

  8. Debug的F5~F8用法

    快捷键(F6)单步执行程序,遇到方法时跳过. 快捷键(F8)执行此断点到最后,进入下一个断点开始之处. 快捷键(F5)单步执行程序,遇到方法时进入. 快捷键(F7)单步执行程序,从当前方法跳出.

  9. [转载]Android View.onMeasure方法的理解

    2013-12-18 10:56:28 转载自http://blog.sina.com.cn/s/blog_61fbf8d10100zzoy.html View在屏幕上显示出来要先经过measure( ...

  10. Linux的三种特殊权限

    1.Suid Set位权限 ●对文件以文件的拥有者身份执行文件 ●对目录无影响 权限设置: ●suid=u+s 2.Sgid Set位权限 ●对文件以文件的组身份执行文件 ●对目录在目录中最新创建的文 ...