/**
* Source : https://oj.leetcode.com/problems/single-number-ii/
*
* 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?
*
*/
public class SingleNumber2 { /**
* 数组中每个元素出现3次,只有一个元素出现一次,找出出现一次的元素
*
* 沿用SingleNumber的方法,比如:{1,1,1,2,2,2,3}
* 01
* 01
* 01
* 10
* 10
* 10
* 11
* ______
* 44 对每一位求和
* 11 每一位对3求余
*
* @param arr
* @return
*/
public int singleNumber (int[] arr) {
int res = 0;
for (int i = 0; i < 32; i++) {
int sum = 0;
int mask = 1<<i;
for (int j = 0; j < arr.length; j++) {
if ((arr[j] & mask) != 0) {
sum ++;
}
}
res |= (sum % 3) << i;
}
return res;
} public static void main(String[] args) {
SingleNumber2 singleNumber2 = new SingleNumber2();
System.out.println(singleNumber2.singleNumber(new int[]{1,1,1,2,2,2,3}) + " == 3");
System.out.println(singleNumber2.singleNumber(new int[]{14,14,14,9}) + " == 9");
} }

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(找出数组中只出现一次的数2)

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

  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 找单个数

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

  6. [LeetCode] 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 (面试题推荐)

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

  8. LeetCode | Single Number II【转】

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

  9. LeetCode——Single Number II

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

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

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

随机推荐

  1. Python-杂物

    1,and和or 在一个bool and a or b语句中,当bool条件为真时,结果是a:当bool条件为假时,结果是b. a = "heaven" b = "hel ...

  2. 利用jquery-barcode.js实现生成条形码

    jquery-barcode官网 js下载地址-github 代码示范(官网上也有) <!DOCTYPE html> <html> <head> <meta ...

  3. Linux系统下,在文件中查找某个字符串

    在normal模式下按下/即可进入查找模式,输入要查找的字符串并按下回车. Vim会跳转到第一个匹配.按下n查找下一个,按下N查找上一个. Vim查找支持正则表达式,例如/vim$匹配行尾的" ...

  4. mysql学习3

    1.索引 索引是表的目录,在查找内容之前可以先在目录中查找索引位置,以此快速定位查询数据.对于索引, 会保存在额外的文件中. 作用: 约束 加速查找 1.1.建立索引 a.额外的文件保存特殊的数据结构 ...

  5. 用gulp-imageisux智图api压缩图片

    ➣ 智图平台是什么? 智图是腾讯ISUX前端团队开发的一个专门用于图片压缩和图片格式转换的平台,其功能包括针对png,jpeg,gif等各类格式图片的压缩,以及为上传图片自动选择最优的图片格式.同时, ...

  6. 初始化git库并配置自动部署

    1.初始化库 git init --bare wap.git 2.配置wap.git/config文件 [core] repositoryformatversion = 0 filemode = tr ...

  7. codeforces 13 b

    给你三根线段判段是否组成了A 条件,两条线段交于端点并且夹角不大于90,第三条线段端点在两条线段上并且划分的大小满足 大:小<4:1 注释很全.(主要是我记不清楚了,,好像过了一个多星期了) # ...

  8. git 服务器搭建与运用

    环境:CentOS 6 为了不影响后面的安装 安装依赖库 yum install curl-devel expat-devel gettext-devel openssl-devel zlib-dev ...

  9. SDL 开发实战(二):SDL 2.0 核心 API 解析

    在上一篇文章 SDL 开发实战(一):SDL介绍及开发环境配置 中,我们配置好了SDL的开发环境,并成功运行了SDL的Hello World 代码.但是可能大部分人还是读不太明白具体Hello Wol ...

  10. [Swift]LeetCode392. 判断子序列 | Is Subsequence

    Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...