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)的更多相关文章

  1. leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)

    136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...

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

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

  3. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

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

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

  5. 【LeetCode】137. Single Number II (3 solutions)

    Single Number II Given an array of integers, every element appears threetimes except for one. Find t ...

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

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

  7. 详解LeetCode 137. Single Number II

    Given an array of integers, every element appears three times except for one, which appears exactly ...

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

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

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

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

随机推荐

  1. 【ASP.NET 插件】分享一个可视化HTML编辑器 CKEditor.NET

    因为公司网站的可视化HTML编辑器IE兼容性问题,js报错不能使用,于是在网上找到了个还行的,图片本地上传的话直接把图片拖到编辑窗口就可以了.这个编辑器是在开源中国看到的,个人觉得还不错! CKEdi ...

  2. JS与Android交互

    一.Android调用JS 2种方法 1.通过WebView的loadUrl 2.通过WebView的evaluateJavascript

  3. Windows 端口占用解决

  4. springboot 停止

    因springboot内嵌tomcat或jetty使得我们没法去操作服务: 因此,常常是服务起来后,要重启时会端口占用,我们只能无情的kill掉端口. 不过spring也设置有配置停止的请求: App ...

  5. mfc cef<转>

    在mfc单文档程序中加入cef: .在BOOL CtestCEFApp::InitInstance()中初始化cef HINSTANCE hInst = GetModuleHandle(NULL); ...

  6. C++中几种测试程序运行时间的方法<转>

    转的地址:https://www.cnblogs.com/silentteen/p/7532855.html 1.GetTickCount()函数 原理: GetTickCount()是获取系统启动后 ...

  7. Spark读取配置(转)

    转自:https://github.com/keepsimplefocus/spark-sourcecodes-analysis/blob/master/markdowns/Spark%E8%AF%B ...

  8. UNITY 多个子MESH与贴图的对应关系

    多个贴图时,与子MESH一一对应,如果多了 ...还待研究如何对应

  9. decode 函数用法

    含义解释: decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译值1)ELSIF 条件=值2 THEN ...

  10. Python正则表达式与re模块介绍

    Python中通过re模块实现了正则表达式的功能.re模块提供了一些根据正则表达式进行查找.替换.分隔字符串的函数.本文主要介绍正则表达式先关内容以及re模块中常用的函数和函数常用场景. 正则表达式基 ...