【leetcode】Single Number II (medium) ★ 自己没做出来....
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的次数!!! 可以对每一位统计,也可以对期望出现的次数统计。
答案来自:http://blog.csdn.net/jiadebin890724/article/details/23306837
class Solution {
public:
int singleNumber(int A[], int n) {
int one=, two=, three=;
for(int i=; i<n; i++){
two |= one&A[i];
one^=A[i];
three=one&two;
one&= ~three;
two&= ~three;
}
return one;
}
};
class Solution {
public:
int singleNumber(int A[], int n) {
int one = , two = , three = ;
for(int i = ; i < n; i++)
{
two |= one&A[i];
one ^= A[i];
three = one&two;
one &= ~three;
two &= ~three;
}
if(n% == )
{
return one;
}
else if(n% == )
{
return two;
}
}
};
【leetcode】Single Number II (medium) ★ 自己没做出来....的更多相关文章
- LeetCode——Single Number II(找出数组中只出现一次的数2)
问题: Given an array of integers, every element appears three times except for one. Find that single o ...
- [LeetCode] Single Number II 单独的数字之二
Given an array of integers, every element appears three times except for one. Find that single one. ...
- LeetCode:Single Number II
题目地址:here 题目大意:一个整数数组中,只有一个数出现一次,其余数都出现3次,在O(n)时间,O(1)空间内找到这个出现一次的数 对于”只有一个数出现一次,其余数出现2次“的情况,很简单,只要把 ...
- [leetcode]Single Number II @ Python
原题地址:http://oj.leetcode.com/problems/single-number-ii/ 题意:Given an array of integers, every element ...
- [Leetcode] single number ii 找单个数
Given an array of integers, every element appears three times except for one. Find that single one. ...
- [LeetCode] Single Number II 位运算
Given an array of integers, every element appears three times except for one. Find that single one. ...
- Leetcode Single Number II (面试题推荐)
还记得<剑指offer>和<编程之美>等书上多次出现的找一个数组中仅仅出现一次的数那个题吗? leetcode也有这道题 链接here 相信大家都知道用异或在O(n)的时间复 ...
- LeetCode | Single Number II【转】
题目:Given an array of integers, every element appears three times except for one. Find that single on ...
- LeetCode Single Number II 单元素2
题意:给一个序列,其中只有1个元素只出现1次,其他的都一定出现3次.问这个出现一次的元素是多少? 思路: (1)全部元素拆成二进制,那么每个位上的1的个数应该是3的倍数,如果不是3的倍数,则ans的这 ...
- LeetCode——Single Number II
Description: Given an array of integers, every element appears three times except for one. Find that ...
随机推荐
- css定位之z-index问题分析
新手先去看看 CSS z-index 属性 CSS z-index 属性的使用方法和层级树的概念 ---------------------------------------------- ...
- 如何在发布博客时插入复杂公式——Open Live Writer
1.http://latex.codecogs.com/eqneditor/editor.php 2.使用Word发布
- PHP预编译处理技术简介
1.提高数据库的效率:减少编译次数,减少连接次数.当出现当量操作sql语句,比如大量将数据插入数据库中,原来的那种单个执行sql语句或者批量执行sql语句的做法,显然是不可行的,因为无论是单个执行还是 ...
- c#后台替换html标签的方法
public static string ReplaceHtmlTag(string html) { string strText = System.Tex ...
- mongoDB- - 2 增、删、改 操作
1.创建数据库 语法:use database 说明:如果database不存在,就会创建database:如果存在就会切换到database 2.查看所有数据库 语法:show dbs; 说明:如果 ...
- hash-3.hashCode
1.有一个类Person,有两个字段age和name,我重写Object类的equal方法来比较两个对象的age和name是否相等,但是不重写hashCode. package com.hash; p ...
- nginx 虚拟主机
基于域名的虚拟主机 创建站点目录 [root@nginx conf]# cd /usr/local/nginx/html/ [root@nginx html]# pwd /usr/local/ngin ...
- 1.xrange和range不要混了,2.range(len(xx))不如用enumerate
range()是列表, xrange()是迭代 >>> a = ['Mary', 'had', 'a', 'little', 'lamb'] >>> for i i ...
- Linux系统运行级别
- SVN版本冲突解决
解决冲突有三种选择: A.放弃自己的更新,使用svn revert(回滚),然后提交.在这种方式下不需要使用svn resolved(解决) B.放弃自己的更新,使用别人的更新.使用最新获取的版本覆盖 ...