【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 ...
随机推荐
- collectionView初始化
collectionView初始化时一定要加layout.不然会报错: UICollectionView must be initialized with a non-nil layout param ...
- Android 页面滑动
1.PagerAdapter适配器 PagerAdapter主要是viewpager的适配器,而viewPager是android.support.v4扩展中新添加的一个强大控件,可以实现控件 ...
- js在前端获取在本地上传图片的尺寸
var MyTest = document.getElementById("upload_img").files[0];var reader = new FileReader(); ...
- sql server还原数据库文件(.bak)常见问题解决办法笔记
还原的时候出现错误:备份集中的数据库备份与现有的数据库不同 SQL Server 2005数据库还原出错错误具体信息为:备份集中的数据库备份与现有的A数据库不同 具体操作如下:第一次:新建了数据库A, ...
- myeclipse设置编码格式的4种情况
(1).设置myeclipse工作空间的编码格式,作用范围最大 window-->preference-->general-->workspace-->text file en ...
- Wcf Client 异常和关闭的通用处理方法
在项目中采用wcf通讯,客户端很多地方调用服务,需要统一的处理超时和通讯异常以及关闭连接. 1.调用尝试和异常捕获 首先,项目中添加一个通用类ServiceDelegate.cs public del ...
- bzoj 2186: [Sdoi2008]沙拉公主的困惑
#include<cstdio> #include<iostream> #define ll long long #define N 10000009 using namesp ...
- django.test.client 使用随记
import os,sys,django; sys.path.append("\\path\\to\\mysite")#ATTETION!,Err: "unable to ...
- partition by
在row_number() over()中的over使用,其后需要配合order by ROW_NUMBER () over (partition by username order by i ...
- lucene4.7学习总结
花了一段时间学习lucene今天有时间把所学的写下来,网上有很多文章但大部分都是2.X和3.X版本的(当前最新版本4.9),希望这篇文章对自己和初学者有所帮助. 学习目录 (1)什么是lucene ( ...