leetcode — single-number-ii
/**
* 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的更多相关文章
- [LeetCode] Single Number II 单独的数字之二
Given an array of integers, every element appears three times except for one. Find that single one. ...
- LeetCode——Single Number II(找出数组中只出现一次的数2)
问题: Given an array of integers, every element appears three times except for one. Find that single o ...
- LeetCode:Single Number II
题目地址:here 题目大意:一个整数数组中,只有一个数出现一次,其余数都出现3次,在O(n)时间,O(1)空间内找到这个出现一次的数 对于”只有一个数出现一次,其余数出现2次“的情况,很简单,只要把 ...
- [leetcode]Single Number II @ Python
原题地址:http://oj.leetcode.com/problems/single-number-ii/ 题意:Given an array of integers, every element ...
- [Leetcode] single number ii 找单个数
Given an array of integers, every element appears three times except for one. Find that single one. ...
- [LeetCode] Single Number II 位运算
Given an array of integers, every element appears three times except for one. Find that single one. ...
- Leetcode Single Number II (面试题推荐)
还记得<剑指offer>和<编程之美>等书上多次出现的找一个数组中仅仅出现一次的数那个题吗? leetcode也有这道题 链接here 相信大家都知道用异或在O(n)的时间复 ...
- LeetCode | Single Number II【转】
题目:Given an array of integers, every element appears three times except for one. Find that single on ...
- LeetCode——Single Number II
Description: Given an array of integers, every element appears three times except for one. Find that ...
- leetcode Single Number II - 位运算处理数组中的数
题目描述: 给定一个包含n个整数的数组,除了一个数出现一次外所有的整数均出现三次,找出这个只出现一次的整数. 题目来源: http://oj.leetcode.com/problems/single- ...
随机推荐
- pyenv global 设置失败 pyenv local 设置就成功了 不知道啥原因
dev@PC-20190309QPVT:/mnt/c/data/htdocs/python/flaskr$ pyenv global 3.6.1dev@PC-20190309QPVT:/mnt/c/d ...
- 对象关系映射 ORM
1.1 作用 MTV框架中包括一个重要的部分,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员的工作量,不需要面对因 ...
- Blocks [POJ3734] [矩阵快速幂]
题意: 有长度为n的一排格子,每个格子里面可以任意填入1,2,3,4四个数字,问1,2都为偶数个的方案 T组数据,每组数据一个n(<=1e9) 样例输入 2 1 2 样例输出 2 6 分析 设d ...
- 按模板批量修改Excel文件内容
Sub 按模板修改Excel文件() Dim MoBanWorkBook As Workbook Set MoBanWorkBook = Application.ActiveWorkbook Dim ...
- vs2012开发基于MFC的ActiveX控件
1.新建工程 2.一直点击下一步,直到出现一下界面,注意红色标注选项,点击完成. 3.进入工程的属性界面,设置工程属性 4.添加对话框资源及其他控件,添加对话框类, 5.设置对话框属性 6.设置Dia ...
- mysql爱之深探测
第一:函数 一:内置函数 MYSQL中提供了很多内置的函数,以下: CHAR_LENGTH(str) 返回值为字符串str 的长度,长度的单位为字符.一个多字节字符算作一个单字符. 对于一个包含五个二 ...
- ssm知识点整理
第1章 resultType和resultMap的区别是什么? MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType ...
- 用递归方法求n的阶乘
代码: #include<iostream> using namespace std; int fact(int n); int main() { int n; loop: cin > ...
- Golang Go Go Go part2:变量及常量声明
三.关键字及内置预声明常量.类型.函数 1.关键字 Go有25个关键字,只能用在语法允许的地方,不能作为名称使用,它们是: break default func ...
- [Swift]LeetCode665. 非递减数列 | Non-decreasing Array
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...