LeetCode_Single Number II
2013-10-03 23:34
471 查看
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?
分析: 统计每一位1出现的个数,最后摸3求余数,1则置最终位为1
class Solution { public: int singleNumber(int A[], int n) { // Note: The Solution object is instantiated only once and is reused by each test case. int res = 0; int x, sum; for(int i = 0; i < 32; ++i){ sum = 0; x = (1<<i); for(int j = 0; j< n; ++j) if(A[j]&x) ++sum; sum = sum%3; if(sum == 1) res |= x; } return res; } };
相关文章推荐
- TCP状态转移图学习总结
- PHP foreach数组循环的一些问题
- poj2481 Cows 树状数组
- 安装gcc
- js异步加载
- 解决ok6410 uboot下ping不通的问题
- Java 类初始化顺序 -- 结合阿里2014校招笔试 Java 附加题
- pdf online document view
- gcc编译时头文件库文件搜索顺序(转)
- UNIX线程
- 动态改变ActionBar菜单项
- [second]Validate Binary Search Tree
- 找工作技巧
- socket编程 (PHP实现)
- 在线个人简历(续)
- common lisp 中一个常量引起的bug - Qwertycen - 博客园
- 线程同步之信号量("旗帜"就是方向,"旗帜"就是形象, "旗帜"就是指挥棒)
- 51单片机入门~chapter1
- 深入理解计算机系统chapter7 学习笔记
- linux C的学习笔记