lintcode 落单的数 II
来源:互联网 发布:ubuntu经常异常假死 编辑:程序博客网 时间:2024/04/28 23:50
落单的数 II
给出3*n + 1 个的数字,除其中一个数字之外其他每个数字均出现三次,找到这个数字。
样例
给出 [1,1,2,3,3,3,2,2,4,1] ,返回 4
挑战
一次遍历,常数级的额外空间复杂度
标签
相关题目
思路:和上一道题一样,还是用位操作,尝试消掉出现3次的数字。
对int的32bit的各个位中1出现的次数进行统计,再对数组中所有元素的逐个进行统计,然后对每个bit对3取余,最终的结果就是只出现一次的数字值,再相加就是只出现过一次的数
class Solution {public: /* * @param A: An integer array * @return: An integer */ int singleNumberII(vector<int> &A) { // write your code here int end=0; for(int i=0;i<32;i++) { int num=0;//每位1出现次数 for(auto n:A) { if(findzero(n,i)) num++; } end+=((num%3)<<i);//再左移恢复原值 } return end; } bool findzero(int i,int k) { if((i>>k)&1)//先右移判断 return true; else return false; }};
阅读全文
0 0
- LintCode-落单的数 II
- lintcode-落单的数 II
- LintCode 落单的数 II
- lintcode,落单的数 II
- Lintcode 落单的数 II
- 落单的数 II-LintCode
- lintcode 落单的数 II
- lintcode-落单的数II-83
- Lintcode—落单的数II
- LintCode:M-落单的数II
- LintCode 83. 落单的数II
- lintcode ----落单的数
- LintCode 落单的数
- lintcode-落单的数
- lintcode,落单的数
- lintcode->落单的数
- LintCode :落单的数
- 落单的数-LintCode
- leetcode 126. Word Ladder II BFS + 反向链表 + DFS
- 计蒜客-判断日期是否符合格式
- Redis作为缓存时与DB的数据一致性维护
- Ubuntu16.4中访问另一台Ubuntu16.4中共享出来的目录
- Java OOP
- lintcode 落单的数 II
- dp(1): 跳台阶问题
- 正则表达式:注册和登录使用
- reflect(反射)
- JS——DOM表单
- Vi 使用技巧
- "齐刘海儿"挡画面?苹果:开发者你们调整界面适配
- Mysql数据库操作(三)----用户权限(DCL)
- Ugly Number II