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,

  1. B0 OR B1 OR B2 = "111........1"
  2. 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的更多相关文章

  1. 【LEETCODE OJ】Single Number

    Prolbem link: http://oj.leetcode.com/problems/single-number/ This prolbem can be solved by using XOR ...

  2. 【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 ...

  3. 【LeetCode OJ】Word Ladder II

    Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...

  4. 【LeetCode OJ】Palindrome Partitioning II

    Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by u ...

  5. 【LeetCode OJ】Word Break II

    Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...

  6. 【Leetcode 136】Single Number

    问题描述:给出一个整数数组,除了一个元素外,其他每个元素都出现了2次,找出只出现1次的元素. int singleNumber(vector<int>& nums); 分析:比较自 ...

  7. 【题解】【位操作】【Leetcode】Single Number II

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  8. 【leetcode78】Single Number II

    题目描述: 给定一个数组,里面除了一个数字,其他的都出现三次.求出这个数字 原文描述: Given an array of integers, every element appears three ...

  9. 【LeetCode 229】Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

随机推荐

  1. 你不知道的JavaScript--DOM基础详解2

    转载:http://blog.csdn.net/i10630226/article/details/49785165 先上几张图简要看看DOM的一些方法属性: 大概这些就是常用的,下面具体聊聊. 节点 ...

  2. SVMtoy

    SVMtoy [label_matrix, instance_matrix] = libsvmread('ex8b.txt'); options = ''; % contour_level = [-1 ...

  3. JGibbLDA、GibbsLDA++问题解决

    LDA(Latent Dirichlet Allocation )主题模型是一种用统计进行文本挖掘的方法,它是pLSA(概率潜在语义分析)主题模型基础上加上贝叶斯框架而得到的模型.目前已应用于自然语言 ...

  4. 从java 转到 c# 知识点

    1. 重写的父类方法 必须是虚方法 用virtual 关键字修饰 而子类必须用 ovrride 关键字 java中不需要,, 2. namespace => packet using => ...

  5. css3内容溢出属性

    overflow是css2.0的属性,css3中新增了overflow-x和overflow-y属性. overflow-x主要是用来定义对水平方向内容溢出的剪切,而overflow-y主要是用来定义 ...

  6. HBase High Level Architecutre

  7. POJ 1011 Sticks dfs,剪枝 难度:2

    http://poj.org/problem?id=1011 要把所给的集合分成几个集合,每个集合相加之和ans相等,且ans最小,因为这个和ans只在[1,64*50]内,所以可以用dfs一试 首先 ...

  8. 让webapi只接受ajax请求

    为了测试先做一个简单的webapi,直接用新建项目时默认的就可以了.   在浏览器中测试request get,得到结果   然后再项目中新建一个AjaxOnly的类   AjaxOnly继承Acti ...

  9. wp8.1 Study1: 页面导航&页面间值传递

    摘要:wp8.1与wp8中很多API是不一样了,wp8.1把以前wp7.x时的api去掉了,更多与win8.1的API相似.比如以下的页面导航和页面之间的值传递 1.页面导航 利用Frame.Navi ...

  10. [网络技术]网关 路由器 OSI

    tracert 1.网关与路由 关键的区别:网关是这样一个网络节点:以两个不同协议搭建的网络可以通过它进行通信.路由器是这样一种设备:它能在计算机网络间收发数据包,同时创建一个覆盖网络(overlay ...