Leetcode:signal_number_ii
一、 题目
给一个数组,当中仅仅有一个数出现一次。其它的数都出现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的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- 20180929 北京大学 人工智能实践:Tensorflow笔记08
https://www.bilibili.com/video/av22530538/?p=28 ———————————————————————————————————————————————————— ...
- 数据库中Select For update语句的解析
----------- Oracle -----------------– Oracle 的for update行锁 键字: oracle 的for update行锁 SELECT-FOR UPDAT ...
- qt 闰年
bool QDate::isLeapYear ( int year ) [static]
- 洛谷 P1454 圣诞夜的极光
P1454 圣诞夜的极光 题目背景 圣诞夜系列~~ 题目描述 圣诞老人回到了北极圣诞区,已经快到12点了.也就是说极光表演要开始了.这里的极光不是极地特有的自然极光景象.而是圣诞老人主持的人造极光. ...
- BZOJ2020: [Usaco2010 Jan]Buying Feed II
[传送门:BZOJ2020] 简要题意: 约翰开车回家,遇到了双十一节,那么就顺路买点饲料吧.回家的路程一共有E 公里,这一路上会经过N 家商店,第i 家店里有Fi 吨饲料,售价为每吨Ci 元.约翰打 ...
- jquery ajax中支持哪些返回类型以及js中判断一个类型常用的方法?
1 jquery ajax中支持哪些返回类型在JQuery中,AJAX有三种实现方式:$.ajax() , $.post , $.get(). 预期服务器返回的数据类型.如果不指定,jQuery 将自 ...
- C# Cache的类方法
public class DataCache { /// <summary> /// 获取当前应用程序指定CacheKey的Cache值 / ...
- 请允许我成为你的夏季——shiro、jdbcInsertall
这两天总是觉得自己被关进了一个大笼子,日子拮据.生活不就是这样吗,一边觉得自己很差劲,一边又想成为一个更好的自己.可那又有什么办法呢,万物皆有裂痕,但那又怎样,那是光照进来的地方啊. 开始学习shir ...
- Facebook下载总结
Facebook是美国的一个社交网络服务网站,至今注册用户已超越20亿,月活用户更是惊人的突破3亿. 这样庞大的一个社交类网站,每日产生的社交数据当然也是非常可观,而这些社交数据,更接近口语,所以是比 ...
- Vuejs2.0构建一个彩票查询WebAPP(1)
说明:本人也是刚接触VUE.js,作为一个学习笔记,旨在与初学者共同学习.其中编程语法错误或者写作水平刺眼,还望轻喷. 使用工具:Visual Studio Code.技术栈为vue2+vuex+ax ...