题目:

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

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

题解:

The code seems tricky and hard to understand at first glance.
However, if you consider the problem in Boolean algebra form, everything becomes clear.

What we need to do is to store the number of '1's of every bit. Since each of the 32 bits follow the same rules, we just need to consider 1 bit. We know a number appears 3 times at most, so we need 2 bits to store that. Now we have 4 state, 00, 01, 10 and 11, but we only need 3 of them.

In this solution, 00, 01 and 10 are chosen. Let 'ones' represents the first bit, 'twos' represents the second bit. Then we need to set rules for 'ones' and 'twos' so that they act as we hopes. The complete loop is 00->10->01->00(0->1->2->3/0).

  • For 'ones', we can get 'ones = ones ^ A[i]; if (twos == 1) then ones = 0', that can be tansformed to 'ones = (ones ^ A[i]) & ~twos'.

  • Similarly, for 'twos', we can get 'twos = twos ^ A[i]; if (ones* == 1) then twos = 0' and 'twos = (twos ^ A[i]) & ~ones'. Notice that 'ones*' is the value of 'ones' after calculation, that is why twos is
    calculated later.


Here is another example. If a number appears 5 times at most, we can write a program using the same method. Now we need 3 bits and the loop is 000->100->010->110->001. The code looks like this:

  1. int singleNumber(int A[], int n) {
  2. int na = 0, nb = 0, nc = 0;
  3. for(int i = 0; i < n; i++){
  4. nb = nb ^ (A[i] & na);
  5. na = (na ^ A[i]) & ~nc;
  6. nc = nc ^ (A[i] & ~na & ~nb);
  7. }
  8. return na & ~nb & ~nc;
  9. }

Or even like this:

  1. int singleNumber(int A[], int n) {
  2. int twos = 0xffffffff, threes = 0xffffffff, ones = 0;
  3. for(int i = 0; i < n; i++){
  4. threes = threes ^ (A[i] & twos);
  5. twos = (twos ^ A[i]) & ~ones;
  6. ones = ones ^ (A[i] & ~twos & ~threes);
  7. }
  8. return ones;
  9. }

I hope all these above can help you have a better understand of this problem. (from here,and author is woshidaishu).

Solution 1 ()

  1. class Solution {
  2. public:
  3. int singleNumber(vector<int>& nums) {
  4. int ones = , twos = ;
  5. for(auto n : nums){
  6. ones = (ones ^ n) & ~twos;
  7. twos = (twos ^ n) & ~ones;
  8. }
  9. return ones;
  10. }
  11. };

【LeetCode】137. Single Number II的更多相关文章

  1. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  2. 【LeetCode】137. Single Number II (3 solutions)

    Single Number II Given an array of integers, every element appears threetimes except for one. Find t ...

  3. 【一天一道LeetCode】#137. Single Number II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【LeetCode】136. Single Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...

  5. 【LeetCode】264. Ugly Number II

    Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...

  6. 【LeetCode】136. Single Number (4 solutions)

    Single Number Given an array of integers, every element appears twice except for one. Find that sing ...

  7. 【LeetCode】264. Ugly Number II 解题报告(Java & Python)

    标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ https://leetcode.com/prob ...

  8. 【LeetCode】136. Single Number

    题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...

  9. 【Leetcode】264. Ugly Number II ,丑数

    原题 Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime facto ...

随机推荐

  1. uiwebview 屏幕自适应 -- 根据 内容适应或者 webview适应

    #import <UIKit/UIKit.h> @interface ViewController : UIViewController<UIWebViewDelegate,UISe ...

  2. WinForm开发----关闭window窗体最好的办法

    最近有一人问道,如何切换窗体.一想到这,我就想,不就是new一个form,然后就show么? 可是我发现,当你控制某个属性的时候,不是不能控制,只是很麻烦而已.有没有好的办法?当然有,咋办? 最简单最 ...

  3. hdu_1226超级密码(BFS)

    超级密码 Problem Description Ignatius花了一个星期的时间终于找到了传说中的宝藏,宝藏被放在一个房间里,房间的门用密码锁起来了,在门旁边的墙上有一些关于密码的提示信息:密码是 ...

  4. Apache转发规则的一点注意

    RewriteRule ^studio/$ book.php?mod=studio 这种目录转发, 正常情况下是没问题的. 但是当根目录下存在一个 studio 目录时, apache就不会转发URL ...

  5. urllib2下载网页的三种方法

    1.最直接的方法 #-*- coding: utf-8 -*- import urllib2 #直接请求 response = urllib2.urlopen('https://www.baidu.c ...

  6. mybatis中查询结果进行分组

    在用mybatis进行数据库查询时,对查询结果进行自动分组,在mapper.xml中的配置有些注意的地方,下面是实际项目中一个例子.在数据库中查询中如下: 在结果集中需要对alarmDate进行分组, ...

  7. DNS 原理入门 (转)

    DNS 是互联网核心协议之一.不管是上网浏览,还是编程开发,都需要了解一点它的知识. 本文详细介绍DNS的原理,以及如何运用工具软件观察它的运作.我的目标是,读完此文后,你就能完全理解DNS. 一.D ...

  8. shiro1

    基于角色的访问控制 RBAC(role based access control),基于角色的访问控制. 比如: 系统角色包括 :部门经理.总经理.(角色针对用户来划分) 系统代码中实现: //如果该 ...

  9. [原创]java WEB学习笔记37:EL表达式(简介,运算符,自动类型转换,保留字,隐含对象)

    1.EL 简介 1)EL 全名为 Expression  Language,它原本是 JSTL  1.0 为方便存取数据所自定义的语言 2)语法:EL 语法很简单,它最大的特点就是使用上很方便:${s ...

  10. 《UNIX网络编程》daytimetcpcli测试

    对于刚刚接触网络的人来说,<UNIX网络编程>中第一个例子(daytimetcpcli)可能就测试不通过.也许你试着继续向后读来,自己写一个服务程序来解决这个问题,但是daytime服务也 ...