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. PATH变量

    PATH变量由系统管理员配置,通常包含如下一些存储系统程序的标准路径: /bin  二进制文件目录,用于存放启动系统时用到的路径 /usr/bin  用户二进制文件目录,用于存放用户使用的标准程序 / ...

  2. Can 't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock '(2) ;

    Can 't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock '(2) ; 如果你在网上search这个 ...

  3. 腾讯优测干货精选|Android双卡双待适配——隐藏在数据库中的那些秘密

    腾讯优测是专业的app自动化测试平台,除了提供兼容性测试,远程真机租用等多维度的测试服务,还有优分享-腾讯内部的移动研发测试干货精选~ 许多APP都希望获取用户通讯录联系人,利用通讯录关系链信息来丰富 ...

  4. 数据库基础知识(1)--数据库php连接

    关系数据库的常用基本术语  数据data 数据库database 数据库管理系统dbms 表(数据表)table 字段field,列column 行row,记录record 数据库操作的基本模式(流程 ...

  5. unique函数的作用

    unique() 去重函数 unique()函数是一个去重函数,STL中unique的函数 unique的功能是去除相邻的重复元素(只保留一个),还有一个容易忽视的特性是它并不真正把重复的元素删除.他 ...

  6. (转)深度学习主机环境配置: Ubuntu16.04+Nvidia GTX 1080+CUDA8.0

      深度学习主机环境配置: Ubuntu16.04+Nvidia GTX 1080+CUDA8.0 发表于2016年07月15号由52nlp 接上文<深度学习主机攒机小记>,这台GTX10 ...

  7. Following a Select Statement Through Postgres Internals

    This is the third of a series of posts based on a presentation I did at the Barcelona Ruby Conferenc ...

  8. KO+bootstrap 模态窗全选绑定

    HTML <div id="modalAreaID01"> <button type="button" class="btn btn ...

  9. How to Failover the ‘Cluster Group’

    If you have more than two nodes in the cluster, you can specify the destination node with the follow ...

  10. 【MySQL】探究之null与not null

    相信很多用了mysql很久的人,对这两个字段属性的概念还不是很清楚,一般会有以下疑问: 我字段类型是not null,为什么我可以插入空值 为毛not null的效率比null高 判断字段不为空的时候 ...