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?

题意:给定数组,除一个数仅出现一次外,其余都出现三次,找到仅出现一次的数字。

思路:这题不能延续single number的解法,但是思路可以向逻辑运算符那边靠。这题我开始没有想出来,这个逻辑运算对我而言是一个大坑。看了Grandyang的博客才明白。这里给出原博客中的解释:用3个整数来表示INT的各位的出现次数情况,one表示出现了1次,two表示出现了2次。当出现3次的时候该位清零。最后答案就是one的值,

ones 代表第ith 位只出现一次的掩码变量;twos 代表第ith 位只出现两次的掩码变量;threes 代表第ith 位只出现三次的掩码变量。

博主的个人理解经验是:写出一个例子,按照程序走一遍。

 class Solution {
public:
int singleNumber(int A[], int n)
{
int one=,two=,three=;
for(int i=;i<n;++i)
{
two |=one&A[i];
one ^=A[i];
three=one&two;
one &=~three;
two &=~three;
}
return one;
}
};

原博客中还给出了Single Number III

[Leetcode] single number ii 找单个数的更多相关文章

  1. LeetCode——Single Number II(找出数组中只出现一次的数2)

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

  2. [LeetCode] Single Number II 单独的数字之二

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

  3. LeetCode:Single Number II

    题目地址:here 题目大意:一个整数数组中,只有一个数出现一次,其余数都出现3次,在O(n)时间,O(1)空间内找到这个出现一次的数 对于”只有一个数出现一次,其余数出现2次“的情况,很简单,只要把 ...

  4. [leetcode]Single Number II @ Python

    原题地址:http://oj.leetcode.com/problems/single-number-ii/ 题意:Given an array of integers, every element ...

  5. LeetCode Single Number II 单元素2

    题意:给一个序列,其中只有1个元素只出现1次,其他的都一定出现3次.问这个出现一次的元素是多少? 思路: (1)全部元素拆成二进制,那么每个位上的1的个数应该是3的倍数,如果不是3的倍数,则ans的这 ...

  6. Leetcode Single Number II (面试题推荐)

    还记得<剑指offer>和<编程之美>等书上多次出现的找一个数组中仅仅出现一次的数那个题吗? leetcode也有这道题 链接here  相信大家都知道用异或在O(n)的时间复 ...

  7. leetcode Single Number II - 位运算处理数组中的数

    题目描述: 给定一个包含n个整数的数组,除了一个数出现一次外所有的整数均出现三次,找出这个只出现一次的整数. 题目来源: http://oj.leetcode.com/problems/single- ...

  8. LeetCode——Single Number(找出数组中只出现一次的数)

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

  9. LeetCode | Single Number II【转】

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

随机推荐

  1. ElasticSearch搜索引擎安装配置中文分词器IK插件

    近几篇ElasticSearch系列: 1.阿里云服务器Linux系统安装配置ElasticSearch搜索引擎 2.Linux系统中ElasticSearch搜索引擎安装配置Head插件 3.Ela ...

  2. bash脚本练习

    练习一: 1.添加5个用户,user1,...,user5: 2.每个用户的密码同用户名,添加密码完成后,不显示命令的执行结果: 3.每个用户添加完成后,都要显示用户某某已添加成功. useradd ...

  3. GameplayKit的GKStateMachine用法与实例

    GKStateMachine 玩家进入GameScene场景中 -> 通过GKStateMachine进入到指定的游戏状态GKState 在GameScene场景中 -> 根据不同的逻辑调 ...

  4. LeetCode 104——二叉树中的最大深度

    1. 题目 2. 解答 如果根节点为空,直接返回 0.如果根节点非空,递归得到其左右子树的深度,树的深度就为左右子树深度的最大值加 1. /** * Definition for a binary t ...

  5. OpenCV学习4-----K-Nearest Neighbors(KNN)demo

    最近用到KNN方法,学习一下OpenCV给出的demo. demo大意是随机生成两团二维空间中的点,然后在500*500的二维空间平面上,计算每一个点属于哪一个类,然后用红色和绿色显示出来每一个点 如 ...

  6. POJ 3525/UVA 1396 Most Distant Point from the Sea(二分+半平面交)

    Description The main land of Japan called Honshu is an island surrounded by the sea. In such an isla ...

  7. 《Linux/UNIX系统编程手册》读书笔记

    2018-1-30 一.UNIX.C语言以及Linux的历史回顾 1. UNIX简史.C语言的诞生 1969年,贝尔实验室的Ken Thompson首次实现了UNIX系统. 1973年,C语言步入成熟 ...

  8. how to install pygraphviz on windows 10 with python 3.6

    Here's what worked for me: Win 7 AMD64 Install MSFT C++ compiler. Install Anaconda for Win AMD64, Py ...

  9. iOS-根据两个经纬度计算相距距离

    CLLocation *orig=[[[CLLocation alloc] initWithLatitude:[mainDelegate.latitude_self doubleValue] long ...

  10. TCP系列37—Keep Alive—1、TCP存活检测

    一.TCP存活(keepalive)检测的背景 对于TCP设计来说,如果一个客户端和服务器端建立连接后,不在进行数据传输,那么这个连接将会一直存在下去,理论上即使中间的路由器崩溃重启.或者中间的网络线 ...