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的进阶版,如果其他的数都出现两次而不是三次,可以将所有的数进行异或,出现偶数次的bit最后会是0,而target数字中为1的bit没有被抵消而保留了下来:

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

 int singleNumber(int A[], int n) {
int res = ;
for(int i = ; i < n; i++){
res ^= A[i];
}
return res;
}

Single Number的本质,就是用一个数记录每个bit出现的次数,如果一个bit出现两次就归0,这种运算采用二进制底下的位操作^是很自然的。Single Number II中,如果能定义三进制底下的某种位操作,也可以达到相同的效果,但是这个东西没有现成的可用。

我们换个思路,Single Number II中想要记录每个bit出现的次数,一个数搞不定就加两个数,用ones来记录只出现过一次的bits,用twos来记录只出现过两次的bits,ones&twos实际上就记录了出现过三次的bits,这时候我们来模拟进行出现3次就抵消为0的操作,抹去ones和twos中都为1的bits。

 int singleNumber(int A[], int n) {
int ones = ;//记录只出现过1次的bits
int twos = ;//记录只出现过2次的bits
int threes;
for(int i = ; i < n; i++){
int t = A[i];
twos |= ones&t;//要在更新ones前面更新twos
ones ^= t;
threes = ones&twos;//ones和twos中都为1即出现了3次
ones &= ~threes;//抹去出现了3次的bits
twos &= ~threes;
}
return ones;

【题解】【位操作】【Leetcode】Single Number II的更多相关文章

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

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

  2. [leetcode]Single Number II @ Python

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

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

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

  4. LeetCode:Single Number II

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

  5. [Leetcode] single number ii 找单个数

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

  6. (Array,位操作)137. Single Number II

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

  7. [LeetCode] Single Number II 位运算

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

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

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

  9. LeetCode | Single Number II【转】

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

  10. LeetCode——Single Number II

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

随机推荐

  1. C#识别验证码技术-Tesseract

    相信大家在开发一些程序会有识别图片上文字(即所谓的OCR)的需求,比如识别车牌.识别图片格式的商品价格.识别图片格式的邮箱地址等等,当然需求最多的还是识别验证码.如果要完成这些OCR的工作,需要你掌握 ...

  2. 测试JdbcTemplate执行SQL语句和存储过程

    我在项目中需要使用到oracle的语句片段和存储过程.下面就是我的测试案例: public class DbTest extends BaseTestCase { @Resource JdbcUtil ...

  3. SQL语句大全(mysql,sqlserver,oracle)

    SQL语句大全 --语句功能--数据操作SELECT --从数据库表中检索数据行和列-selectINSERT --向数据库表添加新数据行-insertDELETE --从数据库表中删除数据行-del ...

  4. P264练习题1.2题

    package 集合; import java.util.*; public class fourteen { public static void main(String[] args) { //1 ...

  5. 交互式报表和工作报表控件Stimulsoft Reports.Fx for Java

    Stimulsoft Reports.Fx for Java是一款Java平台下的报表工具控件,可以为您的应用程序添加交互式报表和工作报表.Java技术可以用于不同的平台.不同的操作系统和不同的硬件, ...

  6. Android 时间戳的转换

    在Android应用中,经常会碰到后台的时间是时间戳而现实的需要今天什么时候,昨天什么时候,就像微博的时间显示一样.现在我上一个把时间戳转换的代码: public static String getT ...

  7. android 禁止viewPager 滑动

    public class ContainerViewPager extends MyViewPager { public ContainerViewPager(Context context, Att ...

  8. 使用OCI向Oracle插入Geometry数据

    使用C/C++操作Oracle数据库,使用OCI可谓是最强大,当然也是最难的方式.Oracle是一个功能复杂而强大的数据库,它可以很好的支持空间数据(Oracle spatial).如何使用OCI向O ...

  9. matlab图像剪裁命令imcrop()

    调用格式: I2=imcrop(I,RECT): X2=imcrop(X,MAP,RECT): RGB2=imcrop(RGB,RECT): 其中,I.X.RGB分别对应灰度图像.索引图像.RGB图像 ...

  10. ASP.NET读取EXCEL文件的三种经典方法(转)

    1.方法一:采用OleDB读取EXCEL文件:  把EXCEL文件当做一个数据源来进行数据的读取操作,实例如下:public DataSet ExcelToDS(string Path) {  str ...