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. }

Single Number II(LintCode)的更多相关文章

  1. [LeetCode] 137. Single Number II (位操作)

    传送门 Description Given an array of integers, every element appears three times except for one, which ...

  2. 【leetcode】Single Number II (medium) ★ 自己没做出来....

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

  3. 【leetcode】Single Number && Single Number II(ORZ 位运算)

    题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...

  4. LeetCode 137 Single Number II(仅仅出现一次的数字 II)(*)

    翻译 给定一个整型数组,除了某个元素外其余的均出现了三次. 找出这个元素. 备注: 你的算法应该是线性时间复杂度. 你能够不用额外的空间来实现它吗? 原文 Given an array of inte ...

  5. LeetCode——Single Number II(找出数组中只出现一次的数2)

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

  6. Leetcode Single Number II (面试题推荐)

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

  7. Continuous Subarray Sum II(LintCode)

    Continuous Subarray Sum II   Given an circular integer array (the next element of the last element i ...

  8. [LeetCode] 260. Single Number III(位操作)

    传送门 Description Given an array of numbers nums, in which exactly two elements appear only once and a ...

  9. LightOJ - 1245 - Harmonic Number (II)(数学)

    链接: https://vjudge.net/problem/LightOJ-1245 题意: I was trying to solve problem '1234 - Harmonic Numbe ...

随机推荐

  1. 51Nod 1014 X^2 Mod P

    注意潜在范围 x*x用long long #include <bits/stdc++.h> using namespace std; typedef long long LL; #defi ...

  2. [Luogu 3966] TJOI 2013 单词

    经典ACAM. 注意单词之间添加字符,以及对重复单词的处理. #include <cstdio> #include <cstring> #include <queue&g ...

  3. Flash Sort

    FlashSort依然类似桶排,主要改进了对要使用的桶的预测,或者说,减少了无用桶的数量从而节省了空间,例如 待排数字[ 6 2 4 1 5 9 100 ]桶排需要100个桶,而flash sort则 ...

  4. yum快速安装gitlab

    安装gitlab前戏使用官方的源,还是比较慢的,gitlab官方提供了一个清华大学的源 新建 /etc/yum.repos.d/gitlab-ce.repo,内容为 源[gitlab-ce]name= ...

  5. sscanf的用法

    sscanf也太好用了8我竟然一直都不知道qaq #include<cstdio> #include<cstdlib> #include<cstring> #inc ...

  6. 【BZOJ】3502 PA2012 Tanie linie

    [算法]贪心,一般DP [题解] --- 胡策k≤10的环状DP做法: 1.钦定法:先确定第一位(可能和第n位)的状态,然后后面正常做DP,显然正确答案是一定会被记录的,因为从整体上看不会有影响. 2 ...

  7. CSS 竖线颜色渐变

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"& ...

  8. 关于 zfs 命令相关介绍

    三种设备:filesystem  volume  snapshot 1.zfs listroot@UA4300D-spa:~/hanhuakai/pro_07/git_0708# zfs listNA ...

  9. Python模块学习 - IPy

    简介 在IP地址规划中,涉及到计算大量的IP地址,包括网段.网络掩码.广播地址.子网数.IP类型等,即便是专业的网络人员也要进行繁琐的计算,而IPy模块提供了专门针对IPV4地址与IPV6地址的类与工 ...

  10. 【Python学习笔记】多版本python使用pip安装第三方库

    不知道是不是有人跟我一样,一直Python2与Python3混着用,然而在cmd中默认的Python版本只有一种,使用 pip install xxx(第三方库名)  只会安装到默认版本上. 而如果需 ...