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 ...
随机推荐
- Lodash js数据操作库
https://lodash.com/docs/4.17.4
- [Luogu 3958] NOIP2017 D2T1 奶酪
题目链接 人生第一篇题解,多多关照吧. 注意事项: 1.多组数据,每次要先初始化. 2.因为涉及到开根,所以记得开double. 整体思路: 建图,判断「起点」与「终点」是否连通. 方法可选择搜索(我 ...
- js高阶函数--判断数据类型、函数胡柯里化;
一.判断数据类型: 常见的判断有typeof.instanceof. constructor. prototype,先来看typeof: var a = "hello world" ...
- GXC 钱包部署
参考: [ 官方 wiki ] 基于 Ubuntu 的 GXC 部署 基础环境 OS: Ubuntu gxc: 官方 [ release 最新版本 ] 下载 release 包(ubuntu) cd ...
- quick-cocos2dx 悬浮节点(NotificationNode)
cocos2dx 开发游戏时,有时某些节点不需要随着场景的切换而销毁.但cocos2dx的机制只允许同时只有一个运行的场景,如果你的所有节点都是依附于这个场景的,那场景的切换必然带来节点的销毁. 比如 ...
- 【ALB学习笔记】基于多线程方式的串行通信接口数据接收案例
基于多线程方式的串行通信接口数据接收案例 广东职业技术技术学院 欧浩源 1.案例背景 在本博客的<[CC2530入门教程-06]CC2530的ADC工作原理与应用>中实现了电压数据采集的 ...
- C#读取txt文件时中文乱码
解决办法 使用GB2312中文字符集 StreamReader reader = new StreamReader(txtUrl, Encoding.GetEncoding("gb2312& ...
- GPU硬件加速
现代浏览器大都可以利用GPU来加速页面渲染.每个人都痴迷于60桢每秒的顺滑动画.在GPU的众多特性之中,它可以存储一定数量的纹理(一个矩形的像素点集合)并且高效地操作这些纹理(比如进行特定的移动.缩放 ...
- windows7_常用操作终端操作
查看环境变量:set 添加环境变量:set aa = 88 删除环境变量:set aa = 查看ip配置:ipconfig 查看全局环境变量配置:path 查看当前目录文件:dir 进入E盘:E: 进 ...
- spring cloud ribbon 断路器
@EnableDiscoveryClient @SpringBootApplication @EnableCircuitBreaker //开启断路器 public class ConsumerMov ...