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 ...
随机推荐
- Unity WebGL 窗口自适应
unity 打包好WebGL后,用文本编辑器编辑打包生成的 index.html 文件 在生成的html里面修改代码 <script type="text/javascript ...
- leetcode笔记:Range Sum Query - Mutable
一. 题目描写叙述 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), ...
- Android入门篇(一)Androidproject的搭建,导入与导出,图标的改动
先说一些题外话吧.这是小珂同学的处女作.可能写的不好,请各位读者见谅.我先讲讲我为什么要写博文,那应该也是机缘巧合,有一次.我问学长一个问题,学长发了一个连接给我,里面是一篇博客.那时我仅仅是看 ...
- windows常用命令有哪些(整理)
windows常用命令有哪些(整理) 一.总结 一句话总结:其实这个好学,只要先弄懂主干,清除主干,那么枝叶的添加逻辑就很清除了 这种多内容的,散乱的,弄清除主干效率就高了 1.windows命令行的 ...
- 3.AngularJS-过滤器
转自:https://www.cnblogs.com/best/p/6225621.html 二.过滤器 使用过滤器格式化数据,变换数据格式,在模板中使用一个插值变量.语法格式如下: {{ expre ...
- POJ 3257 DP
题意: 思路: 用vector存上本出发点能到的地方&成本&有趣指数(用结构体保存) 然后DP就好了 f[i][j]表示到了i 成本为j的有趣指数最大是多少 f[vec[i][k].e ...
- 快速架设OpenStack云基础平台
通常在linux下手工安装openstack比较麻烦,StackOps是一个可以快速安装的Openstack解决方案,首先我们下载StackOps的iso文件(stackops-0.5-b1312-d ...
- ireport 追加新报表
ireport 追加新报表 /* To change this template, choose Tools | Templates * and open the template in the e ...
- css3 列表图片hover左右滚动效果
- Fishing Net
In a highly modernized fishing village, inhabitants there make a living on fishery. Their major tool ...