博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode--Single Number II
阅读量:5031 次
发布时间:2019-06-12

本文共 1281 字,大约阅读时间需要 4 分钟。

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?

 

Have you been asked this question in an interview? 

/**We can use the bit operation to do this problem*If you have a problem to understand the bit operation,*then you can think | , & , ^, ~ as the set operation union, intersection and symmetric difference and set complement respectively.*/public class Solution {    public int singleNumber(int[] A) {        int once = 0;        int twice = 0;        int triple = 0;        for(int i = 0 ; i < A.length ; i++){            //update twice when A[i] is added. if A[i] is in once, then added it to twice,            //otherwise, twice is not needed to updated                 twice |= A[i] & once;           //update once            once = A[i] ^ once;           //triple is exactly the intersection of once and twice            triple = once&twice;           //update once and twice again(remove all integer in triple)             once ^= triple;            twice ^= triple;        }        //return the integer in once or twice        return once | twice;    }}

  

 

转载于:https://www.cnblogs.com/averillzheng/p/3568218.html

你可能感兴趣的文章
实现手机扫描二维码页面登录,类似web微信-第三篇,手机客户端
查看>>
PHP socket客户端长连接
查看>>
7、shell函数
查看>>
【转】Apache Jmeter发送post请求
查看>>
【凸优化】保留凸性的几个方式(交集、仿射变换、投影、线性分式变换)
查看>>
NYOJ-613//HDU-1176-免费馅饼,数字三角形的兄弟~~
查看>>
TFS --- GrantBackup Plan Permissions Error
查看>>
软工作业3:用户体验分析——以“南通大学教务管理系统微信公众号”为例
查看>>
Css:背景色透明,内容不透明之终极方法!兼容所有浏览器
查看>>
我们前端跟后端是怎么合作的
查看>>
mysql存储过程
查看>>
洛谷P2556 [AHOI2002] 黑白图像压缩 [模拟]
查看>>
letecode [136] - Single Number
查看>>
linux下设置固定IP的方法
查看>>
高效的jQuery
查看>>
ubuntu 16.04 (软件应用)-输入法
查看>>
windos7修复引导扇区
查看>>
Leetcode总结之Backtracking
查看>>
Android开发学习之路-图片颜色获取器开发(1)
查看>>
StackExchange.Redis 官方文档(一) Basics
查看>>