一、     题目

给一个数组,当中仅仅有一个数出现一次。其它的数都出现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. [SDOI2011]消防(树的直径)

    [SDOI2011]消防 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超越宇宙的热情, ...

  2. 百度API调用实例

    今天依据需求要从百度API中取出一些数据.这些操作包含:将坐标转换成百度坐标.依据转换的百度坐标进行特定的查询. 有需求的收藏下,免得下次手写浪费时间. 涉及到的操作有:JSON格式的字符解析.HTT ...

  3. oracle之ROWNUM的查询应用

    1 在ORACLE数据库中,ROWNUM是ORACLE数据库为查询结果加入的一个伪列.起始值为1.经常使用来处理查询结果的分页. 2 因为ROWNUM的特殊性,使用时候一般是分三层: 第一层:先进行查 ...

  4. Java基本数据类型及字节

    1.基本类型可以分为三类,字符类型char,布尔类型boolean以及数值类型byte.short.int.long.float.double.数值类型又可以分为整数类型byte.short.int. ...

  5. BZOJ 3781 莫队

    思路:不能再裸的裸题-- //By SiriusRen #include <cmath> #include <cstdio> #include <algorithm> ...

  6. JWT Authentication Tutorial: An example using Spring Boot--转

    原文地址:http://www.svlada.com/jwt-token-authentication-with-spring-boot/ Table of contents: Introductio ...

  7. mkdi---创建目录。

    mkdir命令用来创建目录.该命令创建由dirname命名的目录.如果在目录名的前面没有加任何路径名,则在当前目录下创建由dirname指定的目录:如果给出了一个已经存在的路径,将会在该目录下创建一个 ...

  8. R与并行计算

    本文在Creative Commons许可证下发布 什么是并行计算? 并行计算,准确地说应该包括高性能计算机和并行软件两个方面.不过,近年来随着个人PC机,廉价机群,以及各种加速卡(NVIDIA GP ...

  9. mysql InnoDB加锁分析

    文章转载自:http://www.fanyilun.me/2017/04/20/MySQL%E5%8A%A0%E9%94%81%E5%88%86%E6%9E%90/ 以下实验数据基于MySQL 5.7 ...

  10. Django_shell命令操作