一、     题目

给一个数组,当中仅仅有一个数出现一次。其它的数都出现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. 百度地图API的使用示例

    刚刚工作的时候写过百度地图API文档,那时候没有记录到技术博客里面,今天在群里看见有个姑娘在问这个问题,重温了一遍,这个API还算好用.百度地图API,集成简单好用,全面,兼容问题,文档全面: 官方文 ...

  2. ARM官方《CMSIS-RTOS教程》之线程Threads

    创建线程Creating Threads 一旦RTOS开始运行,就会有很多系统调用来管理和控制活跃的线程.默认情况下,main()函数自动被创建为第一个可运行的线程.在第一个例子里我们使用main() ...

  3. ArcGIS api for javascript——图层-创建定制的切片图层类型的图层

    描述 本例展示了如何创建一个定制图层,该图层访问来自WEB服务器的地图切片.这种方式最通用的用途是检索ArcGIS Server 9.2切片缓存或web上的其他地图切片服务.尽管本例使用的是一个Arc ...

  4. 【HDU 5402】Travelling Salesman Problem(构造)

    被某题卡SB了,结果这题也没读好...以为每一个格子能够有负数就当搜索做了.怎么想也搜只是去,后来发现每一个格子是非负数,那么肯定就是构造题. 题解例如以下: 首先假设nn为奇数或者mm为奇数,那么显 ...

  5. js的一些常用判断小实验

    下面是小实验案例 // 0 if(undefined) { console.log('1'); } else { console.log('0'); } // 0 if(null) { console ...

  6. BZOJ 1598 第k短路

    思路: 先反向建图 Dijkstra一遍 求出h数组 再正向建图 A_star一遍 搞定 //By SiriusRen #include <queue> #include <cstd ...

  7. C# Cache的类方法

    public class DataCache    {        /// <summary>        /// 获取当前应用程序指定CacheKey的Cache值        / ...

  8. 分享一下js正则中惰性与贪婪

    首先引入一个介绍比较详细的网站 http://www.cnblogs.com/yuaima/p/5258513.html http://www.jb51.net/article/31491.htm 接 ...

  9. OpenSUSE Leap 42.3 安装java(Oracle jre)

    一.安装1.从Oracle官网下载jre-8u161-linux-x64.rpm安装包(下载与浏览器位数一样) 2.安装: openSUSE:/home/pipci # rpm -ivh jre-8u ...

  10. 安装SQL2008后需要打开的端口为 1433,4630