Single Number II(LintCode)
Single Number II
Given 3*n + 1 numbers, every numbers occurs triple times except one, find it.
Given [1,1,2,3,3,3,2,2,4,1] return 4
One-pass, constant extra space.
统计每一位上的1出现的次数,然后模3 , 题目上的3 * n + 1给了提示,然后又做过一题2 * n + 1的位操作。
public class Solution {
/**
* @param A : An integer array
* @return : An integer
*/
public int singleNumberII(int[] A) {
int[] bit = new int[32];
for(int a :A) {
for(int i = 0;i<32;i++) {
if(((1 << i) & a) != 0) {
bit[i] = (bit[i] + 1) % 3;
}
}
}
int res = 0;
for(int i=31;i>=0;i--) {
res = res * 2 + bit[i];
}
return res;
}
}
Single Number II(LintCode)的更多相关文章
- [LeetCode] 137. Single Number II (位操作)
传送门 Description Given an array of integers, every element appears three times except for one, which ...
- 【leetcode】Single Number II (medium) ★ 自己没做出来....
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 【leetcode】Single Number && Single Number II(ORZ 位运算)
题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...
- LeetCode 137 Single Number II(仅仅出现一次的数字 II)(*)
翻译 给定一个整型数组,除了某个元素外其余的均出现了三次. 找出这个元素. 备注: 你的算法应该是线性时间复杂度. 你能够不用额外的空间来实现它吗? 原文 Given an array of inte ...
- 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 (面试题推荐)
还记得<剑指offer>和<编程之美>等书上多次出现的找一个数组中仅仅出现一次的数那个题吗? leetcode也有这道题 链接here 相信大家都知道用异或在O(n)的时间复 ...
- Continuous Subarray Sum II(LintCode)
Continuous Subarray Sum II Given an circular integer array (the next element of the last element i ...
- [LeetCode] 260. Single Number III(位操作)
传送门 Description Given an array of numbers nums, in which exactly two elements appear only once and a ...
- LightOJ - 1245 - Harmonic Number (II)(数学)
链接: https://vjudge.net/problem/LightOJ-1245 题意: I was trying to solve problem '1234 - Harmonic Numbe ...
随机推荐
- DataGridView导出到Word
#region 使用Interop.Word.dll将DataGridView导出到Word /// <summary> /// 使用Interop.Word.dll将DataGridVi ...
- HDU 2154 跳舞毯 | DP | 递推 | 规律
Description 由于长期缺乏运动,小黑发现自己的身材臃肿了许多,于是他想健身,更准确地说是减肥. 小黑买来一块圆形的毯子,把它们分成三等分,分别标上A,B,C,称之为“跳舞毯”,他的运动方式是 ...
- c# 事实证明,abstract类除了不能用new实例化和类没什么区别
abstract类是抽象类,不能够实例化,大家都知道,abstract类往往和接口interface一块儿使用,针对接口中一些公共的方法进行实现,然后实体类去继承抽象类和接口.虽然abstract类不 ...
- Robot Framework 自定义关键字 Ignore error
以上是关键字的完整写法. 一下是调用该关键字的实例.
- web-project 故障查看功能 检测是否启动fmd服务
def check_fmd_service(): try: output = subprocess.check_output('svcs -H -o state fmd',shell=True) st ...
- windows+nexus+maven环境搭建(转)
windows nexus环境搭建 1.下载nexus 版本为 nexus-2.11.4-01-bundle 下载地址 这里写链接内容 2.将下载好的文件放到D盘进行解压 3.解压后目录结构 nexu ...
- [一] sqlinject bypass
http://103.238.227.13:10087/?id=1 由源码来看是没有办法注入的,几乎都是过滤了的.但是经过测试加<>符号会被直接替换为空. 那么就可以借助此进行bypass ...
- 【DataScience学习笔记】Coursera课程《数据科学家的工具箱》 约翰霍普金斯大学——Week3 Conceptual Issues课堂笔记
Coursera课程<数据科学家的工具箱> 约翰霍普金斯大学 Week3 Conceptual Issues Types of Questions Types of Data Scienc ...
- [Leetcode Week15] Add Two Numbers
Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...
- python基础===新式类与经典类
首先: Python 2.x中默认都是经典类,只有显式继承了object才是新式类 Python 3.x中默认都是新式类,不必显式的继承object 这两种类的区别: 新式类重定义的方法更多,当然这不 ...