问题描述:

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. LOJ121 「离线可过」动态图连通性

    思路 动态图连通性的板子,可惜我不会在线算法 离线可以使用线段树分治,每个边按照存在的时间插入线段树的对应节点中,最后再dfs一下求出解即可,注意并查集按秩合并可以支持撤销操作 由于大量使用STL跑的 ...

  2. P3727 曼哈顿计划E

    点分治+SG函数还真是令人意外的组合啊 思路 这道题看到找一条满足条件的链,想到点分治 看到博弈,想到SG函数 然后就变成一道SG函数+点分治的题了 然后1e9的SG函数怎么搞?当然是打表了 然后各种 ...

  3. Face Aging with Conditional Generative Adversarial Network 论文笔记

    Face Aging with Conditional Generative Adversarial Network 论文笔记 2017.02.28  Motivation: 本文是要根据最新的条件产 ...

  4. java 偏向锁,轻量锁,重量级锁

    synchronized的执行过程: 1. 检测Mark Word里面是不是当前线程的ID,如果是,表示当前线程处于偏向锁 2. 如果不是,则使用CAS将当前线程的ID替换Mard Word,如果成功 ...

  5. Git-Flow | How it’s used and why you should

    Git-Flow | How it’s used and why you should What is Git-Flow about? Git-Flow is a workflow for using ...

  6. 如何让浏览器不解析html?

    原问题: 在页面中,除了xmp,textarea以及js转义外,还有什么办法可以让html标签在不被浏览器解析而正常显示呢? 答: 要符合“内部的html标签不被解析”,我们根据HTML5的标准,分元 ...

  7. HDU 5844 LCM Walk(数学逆推)

    http://acm.hdu.edu.cn/showproblem.php?pid=5584 题意: 现在有坐标(x,y),设它们的最小公倍数为k,接下来可以移动到(x+k,y)或者(x,y+k).现 ...

  8. Linux——vi的使用

    记录一下vi的一些使用指令,蓝色部分是比较常用的,其中使用过的重新进行了描述,极少部分是未使用过的,还有一些未使用也未记录进来,后续再来补充修正: 参考资料:http://cn.linux.vbird ...

  9. 陌上花开——CDQ分治

    传送门 “CDQ分治”从来都没有听说过,写了这题才知道还有这么神奇的算法. (被逼无奈).w(゚Д゚)w 于是看了不少dalao的博客,对CDQ算法粗浅地了解了一点.(想要了解CDQ的概念,可以看下这 ...

  10. python3使用pymysql模块,连接mysql数据库,实现新增、查询和更新操作

    1.环境数据准备: python3环境.pymysql模块 mysql数据库:本次代码中用到的数据库为本地的testdb数据库,user表(表字段比较简单,只有主键id,手机号mobile,密码pas ...