问题描述:

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?

提示:bit manipulation(位操作)

参考:http://www.acmerblog.com/leetcode-single-number-ii-5394.html?utm_source=tuicool&utm_medium=referral

分析:由于所有数字都是出现奇数次,所以,并不是简单的异或运算。

考虑所有数字用二进制表示,如果把第i个位置上所有数字的和对3取余,那么只会有两种结果,0或1。因此,取余的结果就是那个single number。

java 代码:

一、一个直接的实现就是用大小为 32的数组来记录所有位上的和。

public int singleNumber(int[] nums){

       //所有数字都使用32位二进制表示,初始为0
int[] count = new int[32];
int result = 0; //singlenumber
for(int i = 0; i < 32; i++){
for(int j = 0; j < nums.length; j++){
int key = (nums[j] >> i) & 1;
if (key == 1) { //右移,获得第i个bit,统计1的个数
count[i] ++ ;
}
}
//第i位左移,然后将所有位相或,最终得到singlenumber
result |= ((count[i] % 3) << i);
}
return result;
}

二、使用掩码,改进算法一

  1. ones   代表第ith 位只出现一次的掩码变量
  2. twos  代表第ith 位只出现两次的掩码变量
  3. threes  代表第ith 位只出现三次的掩码变量
public int singleNumber(int[] nums){

      int ones = 0, twos = 0, threes = 0;

      for(int i = 0; i < nums.length; i++){

            twos = twos | ( ones & nums[i]);

            ones = ones ^ nums[i]; //异或3次 和 异或 1次的结果是一样的

            threes = ones & twos;  

            //对于ones 和 twos, 把出现了3次的位置设置为0 (取反之后1的位置为0)

            ones = ones & ~threes;

            twos = twos & ~threes;

      }

      return ones;

}

Single Number II leetcode java的更多相关文章

  1. Single Number III leetcode java

    问题描述: Given an array of numbers nums, in which exactly two elements appear only once and all the oth ...

  2. Ugly Number II leetcode java

    问题描述: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fa ...

  3. Single Number II - LeetCode

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

  4. Leetcode 137 Single Number II 仅出现一次的数字

    原题地址https://leetcode.com/problems/single-number-ii/ 题目描述Given an array of integers, every element ap ...

  5. LeetCode 137. Single Number II(只出现一次的数字 II)

    LeetCode 137. Single Number II(只出现一次的数字 II)

  6. LeetCode(137) Single Number II

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

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

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

  8. 【leetcode】Single Number && Single Number II(ORZ 位运算)

    题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...

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

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

随机推荐

  1. SpringBoot 使用Druid连接池

    1.pom依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  2. SQL优化参考

    1.对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  3. [转] Java中的final、static、this、super

    final 关键字 final关键字主要用在三个地方:变量.方法.类. 对于一个final变量,如果是基本数据类型的变量,则其数值一旦在初始化之后便不能更改:如果是引用类型的变量,则在对其初始化之后便 ...

  4. 题解——洛谷P4767 [IOI2000]邮局(区间DP)

    这题是一道区间DP 思维难度主要集中在如何预处理距离上 由生活经验得,邮局放在中间显然最优 所以我们可以递推求出\( w[i][j] \)表示i,j之间放一个邮局得距离 然后设出状态转移方程 设\( ...

  5. Ubuntu fcitx CPU占用率很高解决方法

    在Ubuntu中,有时候电脑的风扇突然狂装,用 pidstat -u 5 1 命令查看后台应用的资源占用情况,发现fcitx的占用率接近百分之百. 原因是搜狗云输入的问题,关闭后,在用kill命令干掉 ...

  6. 数据库中清空数据,保留表结构的sql语句

    方法一:Delete Form 表名 方法二:TRUNCATE TABLE 在功能上与不带 WHERE 子句的 DELETE 语句相同:二者均删除表中的全部行.但 TRUNCATE TABLE 比 D ...

  7. 【转载】C++宏定义详解

    摘自:http://blog.chinaunix.net/uid-21372424-id-119797.html   C++宏定义详解 2011-02-14 23:33:24   分类: C/C++ ...

  8. jQuery 知识点总结

    jQuery 是一个“写的更少,但做的更多”的轻量级JavaScript 库.对于网页开发者来说,学会jQuery是必要的.因为它让你了解业界最通用的技术,为将来学习更高级的库打下基础,并且确实可以很 ...

  9. vue--toutiao

    git:https://github.com/vinieo/vue-toutiao 顶部导航栏 内容 底部导航按钮 组件

  10. go 接口以及对象的使用

    // Sample program to show how to declare methods and how the Go // compiler supports them. package m ...