一、     题目

给一个数组,当中仅仅有一个数出现一次。其它的数都出现3次,请找出这个数。要求时间复杂度是O(n)。空间复杂度O(1)。

二、     分析

第一次遇见这种题,真心没思路….前面的signal number中我们能够直接异或得到结果。非常显然这个更复杂了。

暴力解法或排序显然无法满足时空要求,所以还得回到位运算上。既然是相同的数出现了三次,我们能够想到他们的二进制表达相应的位置上相同。对于除出现一次之外的全部的整数,其二进制表示中每一位1出现的次数是3的整数倍,将全部这些1清零,剩下的就是终于的数。

假设我们在每个位置上取到出现1的次数,并进行余3操作。则可消除该数。

比如:A[]={2,3,4,3,2,2,3}

0010

0011

0100

0011

0010

0010

0011

=   -0-1-6-3-(1的个数)

0100

int型数据为32位,能够开一个大小为32的int型数组存储N个元素的各个二进制位的1出现的次数,然后将该次数模3运算。

PS:后面几种方法刚開始真心没看懂….自己弱菜

class Solution {
public:
int singleNumber(int A[], int n) {
int bitint[32]={0};
int ans=0;
for(int i=0; i<32; i++){
for(int j=0; j<n; j++){
bitint[i]+=(A[j]>>i)&1;
}
ans|=(bitint[i]%3)<<i;
}
return ans;
}
}; class Solution {
public:
int singleNumber(int A[], int n) {
int one,two,three;
one=two=three=0;
for(int i=0;i<n;i++)
{//一定是出现3次。2次,1次这种顺序,假设反过来的话。先更新了one的话,会影响到two和three的
three = two & A[i];//已经出现了两次,还出现了一次
two = two | one & A[i];//出现了1次又出现了1次,在加上曾经已经出现了2次的,为新的出现了2次的
one = one | A[i];//出现了1次
//将出现3次的其出现1次2次所有抹去
one = one & ~three;
two = two & ~three;
}
return one;
}
}; int sol2(int A[], int n)
{
int one = 0, two = 0, three = 0;
for (int i = 0;i < n;++i) {
two |= one & A[i]; 新的那个数和出现过一次(one) & 就是出现过两次,加到Two里面
one ^= A[i]; 和one ^ 假设没出现过。什么都不会发生;假设出现过,自然消失。
three = one & two; 假设出现第三次的必定是one 和 two的合并内容
one &= ~three; 把第三次出现的从one删除
two &= ~three; 把第三次出现的从two删除
}
return one;
} class Solution {
public:
int singleNumber(int A[], int n) {
int one = 0, two = 0; for (int i = 0; i < n; i++) {
one = (one ^ A[i]) & ~two;
two = (two ^ A[i]) & ~one;
} return one;
}
};

Leetcode:signal_number_ii的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. python异步IO-asyncio、async和await

    参考链接: asyncio:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00 ...

  2. 紫书 例题 10-16 UVa 12230(数学期望)

    感觉数学期望的和化学里面求元素的相对原子质量的算法是一样的 就是同位素的含量乘上质量然后求和得出 这道题因为等待时机是0到2*l/v均匀分配的,所以平均时间就是l/v 再加上过河的l/v, 最后加上步 ...

  3. linux6 安装oracle11g

    转自:http://blog.csdn.net/nhm_lxy/article/details/37813789 安装环境:    虚拟机VMware Workstation 10.0 操作系统:   ...

  4. Maven 工程 POM.XML文件最全详解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  5. 对GPDB查询计划的Motion结点的理解

    GPDB在进行join查询时,可能会产生Motion结点 根据官方文档,总共有这几种Motion: redistribute 重分布(用hash取模的方法把join字段重分布到各个segment,相当 ...

  6. Invalid command &#39;WSGIScriptAlias&#39;, perhaps misspelled or defined by a module not included in the ser

    没有Include wsgi,执行: sudo a2enmod wsgi 可能出现以下的错误 ERROR: Module mod-wsgi does not exist! 安装 libapache2- ...

  7. Struts2 全局结果集

    1.index,jsp <body> Result类型 <ol> <li><a href="user/user?type=1">返回 ...

  8. Jmeter +InfluxDB +collectd +Grafana16

    Jmeter +InfluxDB +collectd +Grafana(十六) 虚拟机ip 192.168.180.128 Influxdb Influxdb是一个开源的分布式时序.时间和指标数据库, ...

  9. intellij idea 13

    mac版 http://pan.baidu.com/s/1c0zjWU8 intellij idea 编辑器之于程序员,犹如鞋之于女人.有的女人赤脚都漂亮,性感. 有的女人赤身都没人看.程序员亦如此. ...

  10. drbd脑裂

    环境: Primary    节点:node1Secondary  节点:node2 DRBD产生脑裂的原因:    (1. 采用HA环境的时候自动切换导致脑裂;    (2. 人为操作或配置失误,导 ...