137. Single Number II (Bit)
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?
思路:用变量分别存储出现一次、两次、三次的number
出现一次的number: 抑或操作去除了出现两次的可能,与未出现三次的number作“与操作”去除了出现三次的可能
出现两次的number:与出现一次的number作“与操作”
出现三次的number: 出现一次的number“与操作”出现两次的number
class Solution {
public:
int singleNumber(int A[], int n) {
int once = ;
int twice = ;
for (int i = ; i < n; i++) {
twice |= once & A[i]; //the num appeared 2 times
once ^= A[i]; //the num appeared 1 times
int not_three = ~(once & twice); //the num not appeared 3 times
once = not_three & once; //remove num appeared 3 times from once
twice = not_three & twice; //remove num appeared 3 times from twice
}
return once;
}
};
137. Single Number II (Bit)的更多相关文章
- leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)
136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...
- LeetCode 137. Single Number II(只出现一次的数字 II)
LeetCode 137. Single Number II(只出现一次的数字 II)
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- Leetcode 137 Single Number II 仅出现一次的数字
原题地址https://leetcode.com/problems/single-number-ii/ 题目描述Given an array of integers, every element ap ...
- 【LeetCode】137. Single Number II (3 solutions)
Single Number II Given an array of integers, every element appears threetimes except for one. Find t ...
- [LeetCode] 137. Single Number II 单独的数字之二
Given a non-empty array of integers, every element appears three times except for one, which appears ...
- 详解LeetCode 137. Single Number II
Given an array of integers, every element appears three times except for one, which appears exactly ...
- LeetCode 137 Single Number II(仅仅出现一次的数字 II)(*)
翻译 给定一个整型数组,除了某个元素外其余的均出现了三次. 找出这个元素. 备注: 你的算法应该是线性时间复杂度. 你能够不用额外的空间来实现它吗? 原文 Given an array of inte ...
- [LeetCode] 137. Single Number II 单独数 II
Given a non-empty array of integers, every element appears three times except for one, which appears ...
随机推荐
- jsp不解析el表达式,不识别jstl标签,找不到http://java.sun.com/jsp/jstl/core
问题描述: jsp页面中el表达式,例如:${pageContext.request.contextPath},原样呈现,未被解析. 解决方案: 为jsp页添加page指令如下: <%@ pag ...
- leetcode 错误题解,反面教材 Gas Station
class Solution { public: int canCompleteCircuit(vector<int>& gas, vector<int>& c ...
- SQL Server CLR 使用 C# 自定义存储过程和触发器
资源来源:https://www.cnblogs.com/Brambling/p/8016060.html SQL Server CLR 使用 C# 自定义存储过程和触发器 这一篇博客接着上一篇博 ...
- XML报错:The reference to entity "characterEncoding" must end with the ';' delimite
解决方法: 在web.xml增加如下配置: <filter> <filter-name>encodingFilter</filter-name> <fil ...
- 转: python requests的安装与简单运用
requests是Python的一个HTTP客户端库,跟urllib,urllib2类似,那为什么要用requests而不用urllib2呢? 官方文档中是这样说明的: python的标准库urlli ...
- 福州大学软件工程W班-助教总结
背景 福州大学软件工程W班,总人数46人,讲师汪老师. 前期期望 希望自己能够在课程当中起到引导作用,发挥助教最大的用处. 实际执行情况 第一个问题是自动化测试工具,该工具主要是用来测试程序WordC ...
- jgGrid
jqGrid的表格加载 function GetGrid() { var selectedRowIndex = 0; var $gridTable = $("#gridTable" ...
- delphi调用LUA函数来处理一些逻辑
替同事做了个洛奇英雄传自动染色程序,关于屏幕取色的. 因为里面他对颜色的要求比较复杂,改动也比较大,于是我让他把逻辑写在 lua 脚本里面. uses LUA, LUALIB; function lu ...
- 24 week 4 安装 docker
安装docker 出现问题 解决办法https://blog.csdn.net/VOlsenBerg/article/details/70140211 发现链接超时,然后就https://blog.c ...
- android布局管理器
1 LinearLayout (线性布局) 让所有的组件都成为单一的方向,机垂直的或水平的(默认). android:Layout_weight //该属性控制水平和垂直方向某个控件所占比例. 2.F ...