Leetcode 137: (Single Number II)
2016-04-13 20:20
447 查看
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?
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
分析
用一个长度为32的数组存贮每位中1出现的次数,由于除去要选择的那个数,其它的数都是出现三次,所以取完次数后%3,所得的结果就是唯一一个没有出现三次的那个数。class Solution { public: int singleNumber(vector<int>& nums) { int count[32]={0}; int result=0; for(int i=0;i<32;i++){ for(int j=0;j<nums.size();j++){ if((nums[j]>>i)&1){ count[i]++; } } result |= (count[i]%3)<<i; } return result; } };
相关文章推荐
- OpenCV读入图片序列进行HOG行人检测并保存为视频
- UVA 10635 Prince and Princess lcs--》lis
- hdu 1058 Humble Numbers
- 解决adb server is out of date. killing。。。 问题
- OpenCV 视频与图片序列相互转换 VideoWriter生成视频流
- css学习
- HDUOJ 1005 Number Sequence(DP求公式)
- 随机生成10个数(使用9种循环)。
- 【Highcharts饼图】动态给饼图添加数据时,绘制“卡住”的情形
- Linux下Camera驱动结构
- SpringMVC核心配置文件
- 100行代码实现Activity右滑退出
- MySQL operation
- Openstack学习笔记(十一)-Horizon学习笔记(一)
- iOS 通讯录拼音检索
- 华为笔试题 (消灭终极boss)
- eclipse中删除tomcat server 导致不能重新创建该server
- 运算符
- 第七周多项目
- Android peferenceActivity 自己定义标题简单方法