Single Number II(LintCode)

Single Number II

Given 3*n + 1 numbers, every numbers occurs triple times except one, find it.

Example

Given [1,1,2,3,3,3,2,2,4,1] return 4

Challenge

One-pass, constant extra space.

 

统计每一位上的1出现的次数,然后模3 , 题目上的3 * n + 1给了提示,然后又做过一题2 * n + 1的位操作。

 1 public class Solution {
 2     /**
 3      * @param A : An integer array
 4      * @return : An integer 
 5      */
 6     public int singleNumberII(int[] A) {
 7         int[] bit = new int[32];
 8         
 9         for(int a :A) {
10             for(int i = 0;i<32;i++) {
11                 if(((1 << i) & a) != 0) {
12                     bit[i] = (bit[i] + 1) % 3;
13                 }
14             }
15         }
16         int res = 0;
17         for(int i=31;i>=0;i--) {
18             res = res * 2 + bit[i];
19         }
20         return res;
21     }
22 }
View Code

 

转载于:https://www.cnblogs.com/FJH1994/p/5031480.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值