【LEETCODE OJ】Single Number
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:
- x XOR 0 is itself, i.e. x XOR 0 = 0;
- x XOR x is 0, i.e. x XOR x = 0;
- 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的更多相关文章
- 【LEETCODE OJ】Single Number II
Problem link: http://oj.leetcode.com/problems/single-number-ii/ The problem seems like the Single Nu ...
- 【Leetcode 136】Single Number
问题描述:给出一个整数数组,除了一个元素外,其他每个元素都出现了2次,找出只出现1次的元素. int singleNumber(vector<int>& nums); 分析:比较自 ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- 【LeetCode】【Python题解】Single Number & Maximum Depth of Binary Tree
今天做了三道LeetCode上的简单题目,每道题都是用c++和Python两种语言写的.由于c++版的代码网上比較多.所以就仅仅分享一下Python的代码吧,刚学完Python的基本的语法,做做Lee ...
- 【LeetCode OJ】Maximum Depth of Binary Tree
Problem Link: https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ Simply BFS from root an ...
- 【LeetCode OJ】Triangle
Problem Link: http://oj.leetcode.com/problems/triangle/ Let R[][] be a 2D array where R[i][j] (j < ...
- 【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 ...
- 【LeetCode OJ】Word Ladder I
Problem Link: http://oj.leetcode.com/problems/word-ladder/ Two typical techniques are inspected in t ...
随机推荐
- WCF中Service Configuration Editor的使用方法
1.在App.config文件上右击,选择Edit WCF Configuration.... 或者打开Program Files\Microsoft Visual Studio 8\Common7\ ...
- PHP 高并发、抢票、秒杀 解决方案
对于抢票.秒杀这种业务,我说说自己对这种高并发的理解吧,这里提出个人认为比较可行的几个方案:方案一:使用队列来实现可以基于例如MemcacheQ等这样的消息队列,具体的实现方案这么表述吧比如有100张 ...
- 网络编程之socket(转)
“一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价 值,那网络中进程之间如何通信,如我们每天打开浏 ...
- servlet 配置
<servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>com.web.s ...
- Java 字典排序
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import org.ju ...
- HTML 基础知识——8月8日
一.基础知识: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...
- Help Me Escape (ZOJ 3640)
J - Help Me Escape Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:32768KB ...
- Windows 下对目录建立软链接
在Linux下,可以很方便的通过ln命令创建对文件和对文件夹的软链接.在Windows下,通过快捷方式,可以创建到文件和文件夹的链接,但是这软链接不是一个层面的上东西.软链接是底层文件系统层面的,而快 ...
- 执行MAVEN更新包
我们一般使用 mvn eclipse:eclipse 执行对maven库的引用,这样会修改项目下的classpath文件. 我们修改直接在eclipse 使用maven库作为项目的引用. 步骤如下: ...
- 下拉刷新列表添加SwipeDismissListViewTouchListener实现滑动删除某一列。
<Android SwipeToDismiss:左右滑动删除ListView条目Item> Android的SwipeToDismiss是github上一个第三方开源框架(github上的 ...