LeetCode:Single NumberⅡ
2015-07-14 09:52
363 查看
Problems:
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?
解法一:按位统计0、1出现的次数。使用每个数的每位相加,取模运算,来获得结果。
The second solution is need update.
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?
解法一:按位统计0、1出现的次数。使用每个数的每位相加,取模运算,来获得结果。
class Solution { public: int singleNumber(vector<int>& nums) { const int N=sizeof(int)*8; int count ; fill_n(&count[0],N,0); for(int i=0;i<nums.size();i++) for(int j=0;j<N;j++) { count[j]+=(nums[i]>>j)&1; count[j]%=3; } //返回结果 int result=0; for(int j=0;j<N;j++) result+=(count[j]<<j); return result; } };
The second solution is need update.
相关文章推荐
- libevent安装及使用
- JS限制用户只能输入数字
- 四年了, 一个java程序员的总结
- Android-无法隐藏标题栏
- Cannot run program \"cd\": error=2, No such file or directory
- MFC的工具条和状态栏
- android on qt 之模拟jquery-mobile控件的box-shadow效果
- UICollectionView 集合视图 的使用
- 目前开发手机app的几种方式
- 判断电话号码是否正确
- Jni 简单实战 获取c++返回值
- HDU 1239 Calling Extraterrestrial Intelligence Again
- Composite模式
- jQuery 常用的代码片段
- 黑马程序员--多线程
- ANDROID AIDL(安桌接口定义语言)
- C++使用libcurl做HttpClient
- Android中的线程与进程之间的关系简单解释
- UIKeyboard键盘相关知识点-IOS开发
- Asp.net mvc4 发布你的程序到IIS7.0