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. (27)odoo 中改变菜单动作的默认视图

    一个动作下面有多个视图来支持,像表单视图.列表视图.看板视图.图表视图等 这时我们想改变系统默认指定的视图,方法其实有两种,一种是通过面板改,一种是开发一个小模块 举一例:项目默认打开是用了看板视图, ...

  2. 2016年31款轻量高效的开源JavaScript插件和库

    目前有很多网站设计师和开发者喜欢使用由JavaScript开发的插件和库,但同时面临一个苦恼的问题:它们中的大多数实在是太累赘而且常常降低网站的性能.其实,其中也有不少轻量级的插件和库,它们不仅轻巧有 ...

  3. ecshop数据库取数据

    取出所有数据: test_getAll(); function test_getAll() { global $db; $sql = "SELECT user_id, user_name, ...

  4. [整理]Linux压缩与解压缩命令整理。

    一.压缩文件命令 1.*.Z compress 程序压缩的档案:2.*.bz2 bzip2 程序压缩的档案:3.*.gz gzip 程序压缩的档案:4.*.tar tar 程序打包的数据,并没有压缩过 ...

  5. Git 提交大文件提示 fatal: The remote end hung up unexpectedly

    使用gitlab搭建的git server,如果直接使用http的方式去提交的话,提交小文件不会有问题,但是提交大文件时,会出错: fatal: The remote end hung up unex ...

  6. 搜索功能demo

    代码如下: <html> <head> <meta http-equiv="X-UA-Compatible" content="IE=edg ...

  7. Asynchttpclient开源框架下载图片和文本,于Volley和Glide开源框架的区别。

    AsyncHttpClient是一款比较流行的Android异步网路加载库,在github上的网址是:https://github.com/loopj/android-async-httpAsyncH ...

  8. .NET C#使用微信公众号登录网站

    适用于:本文适用于有一定微信开发基础的用户 引言:花了300大洋申请了微信公众平台后,发现不能使用微信公众号登录网站(非微信打开)获得微信帐号.仔细研究后才发现还要再花300大洋申请微信开放平台才能接 ...

  9. S1:动态方法调用:call & apply

    js中函数执行的两种方式:一是通过调用运算符’()’,二是通过调用call或apply来动态执行. 一.动态方法调用中指定this对象 开发中我们往往需要在对象B中调用对象A的方法,这个时候就用到了a ...

  10. ASP.NET MVC 4使用Bundle的打包压缩JS/CSS

    打包(Bundling)及压缩(Minification)指的是将多个js文件或css文件打包成单一文件并压缩的做法,如此可减少浏览器需下载多个文件案才能完成网页显示的延迟感,同时通过移除JS/CSS ...