137. 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?

数组仅有一个数出现一次,其他的出现3次。找出那个出现一次的数。依然使用位运算来求解。

统计每一位上1出现的次数,1出现次数不为3的倍数的位所组成的数即为要找的数。

代码实现:

 class Solution {
public:
int singleNumber(vector<int>& nums) {
int n = nums.size();
int ones = ;
int twos = ;
int xthrees = ;
for(int i = ; i < n; ++i)
{
twos |= (ones & nums[i]);
ones ^= nums[i];
xthrees = ~(ones & twos);
ones &= xthrees;
twos &= xthrees;
}
return ones;
}
};

leetcode 137的更多相关文章

  1. Leetcode 137. 只出现一次的数字 II - 题解

    Leetcode 137. 只出现一次的数字 II - 题解 137. Single Number II 在线提交: https://leetcode.com/problems/single-numb ...

  2. LeetCode 137. Single Number II(只出现一次的数字 II)

    LeetCode 137. Single Number II(只出现一次的数字 II)

  3. Leetcode 137 Single Number II 仅出现一次的数字

    原题地址https://leetcode.com/problems/single-number-ii/ 题目描述Given an array of integers, every element ap ...

  4. [LeetCode] 137. Single Number II 单独的数字之二

    Given a non-empty array of integers, every element appears three times except for one, which appears ...

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

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

  6. [LeetCode] 137. Single Number II 单独数 II

    Given a non-empty array of integers, every element appears three times except for one, which appears ...

  7. Java实现 LeetCode 137 只出现一次的数字 II(二)

    137. 只出现一次的数字 II 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现了三次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空 ...

  8. leetcode 137[转]

    没思路.网上找到的. 1. 将每一个int看成32位数,统计每一位出现的次数对3取余,所以需要开辟一个32大小的数组来统计每一位出现的次数 2. 对第一种思路进行简化,模拟3进制: three two ...

  9. Leetcode 137. Single Number I/II/III

    Given an array of integers, every element appears twice except for one. Find that single one. 本题利用XO ...

随机推荐

  1. UVA 10816 + HDU 1839 Dijstra + 二分 (待研究)

    UVA 题意:两个绿洲之间是沙漠,沙漠的温度不同,告诉起点,终点,求使得从起点到终点的最高温度最小的路径,如果有多条,输出长度最短的路径: 思路:用最小费用(最短路径)最大流(最小温度)也能搞吧,但因 ...

  2. Bubble Sort_树状数组

    Problem Description P is a permutation of the integers from 1 to N(index starting from 1).Here is th ...

  3. Windows 下字节转换

    Windows 下字节转换 #include <string> #include <windows.h> class CharsConversion { public: sta ...

  4. linux下安装memcache(php版本5.3)

    1.安装之前需要的准备:所需软件 libevent-1.4.6这个版本网上没找到,所以安装最新的版本libevent-2.0.22 libevent-1.4.6-stable.tar.gz (http ...

  5. crontab不能正确执行的问题

    近期在部署crontab任务的时候,总是遇到在shell中单独执行正常,但是放到crontab定时执行出错的问题.若出现这类场景,九成就是环境变量的问题. 因为我的定制任务,基本上都需要使用sqlpl ...

  6. Neo4j 两种索引Legacy Index与Schema Index区别

    Legacy Indexes 在Neo4j 2.0版本之前,Legacy index被称作indexes.这个索引是通过外部图存储在外的Lucene实现,允许“节点”和“联系”以key:value键值 ...

  7. Erlang 104 OTP

    笔记系列 Erlang环境和顺序编程Erlang并发编程Erlang分布式编程YawsErlang/OTP 日期              变更说明 2014-12-21 A Outline, 1 A ...

  8. 通过SSH远程使用ipython notebook

    本文讲述如何在本地用浏览器运行远程服务器上的iPython notebook服务. 在远程机器上,启动IPython notebooks服务: remote_user@remote_host$ ipy ...

  9. Linux——常用命令详解

    文件处理命令:ls  -l drwxr-xr-x   文件 d   rwx  r-x   r-x d:表示directory  是一个目录         - 表示二进制文件    l 表示链接文件l ...

  10. 桥接模式(Bridge)

    桥接模式(Bridge),将抽象部分与它的实现部分分离,使它们都可以独立地变化.Bridge 模式把角色之间的继承关系改为了耦合的关系,从而使这两者可以从容自若的各自独立的变化: 在以下的情况下应当使 ...