问题:

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 I 升级版,一个数组中其它数出现了3次,一个数只出现了一次,找出那个数。

分析:

问题I中是其它数出现了2次,一个数只出现了1次,找到出那个数,当时是使用了异或运算,轻松解决了问题。

在问题II中题目变成了,其它数出现了3次,一个数只出现了一次,找出那个数,再简单的使用异或运算显然不能决绝这个问题,因为其它数也是出现了单数次。

 

在这里,提出了一种可以解决问题的解决方案,但是不是很好,大神们用了一种更好的方法,这里在后面会贴出来,不过由于还没太理解,这里就先不做论述了,那接下来就简单的谈谈这个不是很好的方法。

 

由于其它数都出现了3次,一个数只出现了一次,从bit上来考虑那就是,每个出现了3次的数在每位上都出现了3次,这里以13为例:

1 2 3 4 5 6 7 8
0 0 0 0 1 1 0 1

 

那么3个13的话,每一位分别求和,那么一定是3的倍数,

例如:

位1,加了之后和为0,3的零倍;

位5,6,8,和都为3,3的1倍。

所以,我们可以分别求出数组中每一位所有数的和,然后对3求余就是只出现1次的数在当前位的值(0或1)。

 


代码:

public class Solution {
public int singleNumber(int[] a) {
int result = 0;
for(int i=31;i>=0;i--){
int temp = 0;
result = result<<1;
for(int j=0;j<a.length;j++){
temp += (a[j]>>i) & 1;
}
result |= temp%3;
}
return result;
}
}

 

大神的代码:

public class Solution {
public int singleNumber(int[] A) {
int ones = 0, twos = 0;
for(int i = 0; i < A.length; i++){
ones = (ones ^ A[i]) & ~twos;
twos = (twos ^ A[i]) & ~ones;
}
return ones;
}
}

LeetCode——Single Number II(找出数组中只出现一次的数2)的更多相关文章

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

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

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

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

  3. 一个整型数组里除了一个数字之外,其他的数字都出现了两次。要求时间复杂度是O(n),空间复杂度是O(1),如何找出数组中只出现一次的数字

    思路分析:任何一个数字异或它自己都等于0,根据这一特性,如果从头到尾依次异或数组中的每一个数字,因为那些出现两次的数字全部在异或中抵消掉了,所以最终的结果刚好是那些只出现一次的数字. 代码如下: #i ...

  4. 229. Majority Element II -- 找出数组中出现次数超过 ⌊ n/3 ⌋ 次的数

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  5. [LeetCode] Find All Numbers Disappeared in an Array 找出数组中所有消失的数字

    Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...

  6. 1. 找出数组中的单身狗OddOccurrencesInArray Find value that occurs in odd number of elements.

    找出数组中的单身狗: 1. OddOccurrencesInArray Find value that occurs in odd number of elements. A non-empty ze ...

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

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

  8. 【Java】 剑指offer(1) 找出数组中重复的数字

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字 ...

  9. 《剑指offer》第三_一题(找出数组中重复的数字,可改变数组)

    // 面试题3(一):找出数组中重复的数字 // 题目:在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字重复了, // 也不知道每个数字重复了几次.请 ...

随机推荐

  1. GitHub新手快速入门日常操作流程

    GitHub新手快速入门日常操作流程 1. 注册帐号 打开https://github.com/,填写注册信息并提交. 2. 登录帐号 打开https://github.com/login,输入注册的 ...

  2. 给td添加滚动条

    <td><div style="overflow-y:scroll;height:330px;">test</div></td>

  3. ResourceManager里面Trackingui需要手动该ip

    C:\Windows\System32\drivers\etc 这个路径下配置了ip和主机名,不过是大小写,ping不同,不论ping大小写还是全部小写都不行,我看地址栏是小写所以想着把hosts里C ...

  4. svn清理失败且路径显示乱码

    1.下载 sqlite数据库工具,sqlite3.exe下载地址:sqlite官网http://www.sqlite.org/download.html,我这里是windows操作系统,因此下载 Pr ...

  5. npm命令教程

    教程:http://www.runoob.com/nodejs/nodejs-npm.html 常用命令:http://www.cnblogs.com/PeunZhang/p/5553574.html

  6. IndentationError: unindent does not match any outer indentation level

    [problem] 从别处copy过来的python代码经过自己改动后,运行出错 [解决过程] vim file :set list  # cat -A file 也可以 可以看到9-12行的inde ...

  7. 项目中遇到的关于兄弟controller之间传值的问题解决

    层级关系如下 <ons-page ng-controller="tabbarIndexController"> <ons-tabbar position=&quo ...

  8. Node实践之一

    大家都知道JavaScript的专长就是处理客户端也就是与浏览器打交道了,所有的与服务器端的交互必须交给后台语言处理程序去做,基于JavaScript不能与服务器进行直接交互这样一个现状,Ryan D ...

  9. PHP文件大小格式化函数合集

    比如碰到一个很大的文件有49957289167B,大家一看这么一长串的数字后面单位是字节B,还是不知道这个文件的大小是一个什么概念,我们把它转换成GB为单位,就是46.53GB.用下面这些函数就可以完 ...

  10. Java框架--jQueryEasyUI

    111------------------------------------------------------------------------------------------------- ...