【LEETCODE OJ】Single Number II
Problem link:
http://oj.leetcode.com/problems/single-number-ii/
The problem seems like the Single Number. Suppose we have following (3m+1) numbers in the array A:
x0, x1, x1, x1, ..., xm, xm, xm
We are asked to find out the value of x0.
However we cannot solve it using XOR since all xi has odd number of appearances.
If we consider the problem as an automata, each time we scan a number from A and update the status of the automata. We consider a integer as an array of bits, and for each number we count the number of "1" for each bit of an integer. Then, after scanning the (3m+1) numbers, for i-th bit, there would be 3k+1 1's (and 3(m-k) 0's) if x0's i-th bit is "1"; otherwise there would be 3k 1's (and 3(m-k)+1 0's). Therefore, if we set the bits having (3k+1) 1's to 1 and other bits to 0, this number should be the same to x0.
We use 3 integers to represent the status of the automata, let's say B0, B1, and B2. For j=0,1,2 and i-th bit, Bj[i] = 1 means the automata has scanned 3k+j numbers with i-th bit set to 1. For each bit there is and only is one "1" in B0, B1, and B2 at any time. That is,
- B0 OR B1 OR B2 = "111........1"
- Bi AND Bj = 0 for i ≠ j
The initial status is as follows,
B0: 1111........1
B1: 0000........0
B2: 0000........0
since there is no numbers scanned yet. Then for each number x, we move the common 1's of x and B0 from B0 to B1, common 1's of x and B1 from B1 to B2, and common 1's of x and B2 from B2 to B0. After scanning all numbers, the value of B1 equals to x0 as we mentioned./p>
The python code is as follows.
class Solution:
# @param A, a list of integer
# @return an integer
def singleNumber(self, A):
"""
Similar to Single Number I,
suppose we have 3m+1 numbers where n0 is the single number:
x0, x1, x1, x1, x2, x2, x2, ..., xm, xm, xm.
We are asked to find out the value of x0.
"""
# special cases:
n = len(A)
assert n > 0
if n == 1:
return A[0] # Bit-counters, with following properties
# 1) bc0 OR bc1 OR bc2 = ~0
# 2) bc0 AND bc1 = 0
# 3) bc1 AND bc2 = 0
# 4) bc0 AND bc2 = 0
bc0 = ~0 # bc0's i-th bit is 1 means there are 3k numbers with i-th bit of 1
bc1 = 0 # bc1's i-th bit is 1 means there are 3k+1 numbers with i-th bit of 1
bc2 = 0 # bc2's i-th bit is 1 means there are 3k+2 numbers with i-th bit of 1 # Scan numbers in A
for x in A:
# Commen bits of bc0 and x
c0 = bc0 & x
# Commen bits of bc1 and x
c1 = bc1 & x
# Commen bits of bc2 and x
c2 = bc2 & x
# Move bits of 1 in c0 from bc0 to bc1,
bc0 &= (~c0)
bc1 |= c0
# Move bits of 1 in c1 from bc1 to bc2
bc1 &= (~c1)
bc2 |= c1
# Move bits of 1 in c2 from bc2 to bc0
bc2 &= (~c2)
bc0 |= c2 return bc1
Actually, we do not need all bc0, bc1, and bc2, since one can be computed from the other two. If then, we only need to update two variables each time and compute another one on-fly. However, I prefer to using 3 variables which is easy to read and understand.
【LEETCODE OJ】Single Number II的更多相关文章
- 【LEETCODE OJ】Single Number
Prolbem link: http://oj.leetcode.com/problems/single-number/ This prolbem can be solved by using XOR ...
- 【LeetCode OJ】Path Sum II
Problem Link: http://oj.leetcode.com/problems/path-sum-ii/ The basic idea here is same to that of Pa ...
- 【LeetCode OJ】Word Ladder II
Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...
- 【LeetCode OJ】Palindrome Partitioning II
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by u ...
- 【LeetCode OJ】Word Break II
Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...
- 【Leetcode 136】Single Number
问题描述:给出一个整数数组,除了一个元素外,其他每个元素都出现了2次,找出只出现1次的元素. int singleNumber(vector<int>& nums); 分析:比较自 ...
- 【题解】【位操作】【Leetcode】Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 【leetcode78】Single Number II
题目描述: 给定一个数组,里面除了一个数字,其他的都出现三次.求出这个数字 原文描述: Given an array of integers, every element appears three ...
- 【LeetCode 229】Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
随机推荐
- ooize简介
在Hadoop中执行的任务有时候需要把多个Map/Reduce作业连接到一起,这样才能够达到目的.[1]在Hadoop生态圈中,有一种相对比较新的组件叫做Oozie[2],它让我们可以把多个Map/R ...
- F-Dining Cows(POJ 3671)
Dining Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7584 Accepted: 3201 Descr ...
- 百度Tera数据库介绍——类似cassandra,levelDB
转自:https://my.oschina.net/u/2982571/blog/775452 设计背景 百度的链接处理系统每天处理万亿级的超链数据,在过去,这是一系列Mapreduce的批量过程,对 ...
- AES加密 16进制与二进制转换
import java.security.Key; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax ...
- Servlet+JSP+JavaBean开发模式(MVC)介绍
好伤心...写登陆注册之前看见一篇很好的博文,没有收藏,然后找不到了. 前几天在知乎上看见一个问题,什么时候感觉最无力. 前两天一直想回答:尝试过google到的所有solve case,结果bug依 ...
- 部署步骤“回收 IIS 应用程序池”中出现错误: <nativehr>0x80070005</nativehr><nativestack></nativestack>拒绝访问。
解决方法:以sharepoint管理员身份进入主站点,修改站点的网站集管理员.
- sql语句查询重复的数据
查找所有重复标题的记录: SELECT *FROM t_info aWHERE ((SELECT COUNT(*)FROM t_infoWHERE Title = a.Title) > 1)OR ...
- S1:变量
接触JS一段时间了,但总感觉不得要领,技术得不到提升,翻来覆去,决定对基础知识做一次系统的整理,要坚持每一天都有新的收获 ! 变量,即通过一个名字将一个值关联起来,以后通过变量就可以引用到该值,比如: ...
- ROS语音识别
一.语音识别包 1.安装 安装很简单,直接使用ubuntu命令即可,首先安装依赖库: $ sudo apt-get install gstreamer0.10-pocketsphinx ...
- BPM的四大主要类型
随着网络的发展,移动BPM.社交BPM.云端BPM将顺应市场需求,成为BPM发展的新趋势,最终成为企业即时管控有效工具.BPM将不断促进制造业信息化的转型与发展.所以很少人会否认业务流程管理(BPM) ...