Single Number I&& II——还没看,倒过头来再看
Single Number I
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
直接异或解决。
class Solution {
public:
int singleNumber(int A[], int n) {
int res=;
for(int i=;i<n;i++)
{
res=res^A[i];
}
return res;
}
};
Single Number II
Given an array of integers, every element appears three times except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
分析1
由于int型由32bit表示,因此可以用一个长度为32的int数组保存各个比特位上1出现的次数。最后,将数组各元素对3取余,那么32位数组就纪录了只出现了一次的整数的二进制表示。
public class SingleNumberII {
private final static int INTEGER_DIGITS = ;
public int singleNumber(int[] A) {
if (A == null || A.length <= ) {
return ;
}
int ret = ;
int[] count = new int[INTEGER_DIGITS];
for (int i = ; i < INTEGER_DIGITS; ++i) {
for (int j = ; j < A.length; ++j) {
count[i] += (A[j] >> i) & 0x0001;
}
ret |= (count[i] % ) << i;
}
return ret;
}
}
分析2
我们也没必要开 int bit[32]的数组去统计的
我们来模拟二进制加法
用两位来计数,到3就把该位清零。
bit2 bit1
bit1 如果该位来的是1 ,保持0,1,0,1。。。(也就是xor,异或),如果是0就不变
当bit1=1的时候再来一个1,那么bit2=1
当这两个bit同时为1,说明这是3啦(二进制你想想嘛),该位清零。
class Solution {
public:
int singleNumber(int A[], int n) {
int ones = , twos = , threes = ;
for(int i = ; i < n; i++)
{
threes = twos & A[i]; //已经出现两次并且再次出现
twos = twos | ones & A[i]; //曾经出现两次的或者曾经出现一次但是再次出现的
ones = ones | A[i]; //出现一次的
twos = twos & ~threes; //当某一位出现三次后,我们就从出现两次中消除该位
ones = ones & ~threes; //当某一位出现三次后,我们就从出现一次中消除该位
}
return ones; //twos, threes最终都为0.ones是只出现一次的数
}
};
分析3:
其实就是设置三个标志位,出现一次标志位1对应的bit变为1,出现两次标志位2对应的bit变为1,出现三次标志位三对应的bit变为1.
理解了这种思路,代码也就不难写了。
class Solution {
public:
int singleNumber(vector<int>& nums) {
int one=;
int two=;
int three=;
for(int i=;i<nums.size();i++)
{
two|=one&nums[i];
one^=nums[i];
three=one&two;
one&=~three;
two&=~three;
}
return one|two;
}
};
Single Number I&& II——还没看,倒过头来再看的更多相关文章
- 4.Single Number && Single Number (II)
Single Number: 1. Given an array of integers, every element appears twice except for one. Find that ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- LeetCode Single Number I II Python
Single Number Given an array of integers, every element appears twice except for one. Find that sing ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- Leetcode 137. Single Number I/II/III
Given an array of integers, every element appears twice except for one. Find that single one. 本题利用XO ...
- LeetCode: Single Number I && II
I title: Given an array of integers, every element appears twice except for one. Find that single on ...
- single number i && ii && iii
Problem statement Elementary knowledge: There is a popular question when I seeked my job at Beijing: ...
- LeetCode 【Single Number I II III】
Given an array of integers, every element appears twice except for one. Find that single one. 思路: 最经 ...
- 【深入理解JAVA虚拟机】第4部分.程序编译与代码优化.1.编译期优化。这章编译和实战部分没理解通,以后再看。
1.概述 1.1.编译器的分类 前端编译器:Sun的Javac. Eclipse JDT中的增量式编译器(ECJ)[1]. 把*.java文件转变成*.class文件 JIT编译器:HotSpot ...
随机推荐
- bzoj3232圈地游戏——0/1分数规划+差分建模+判环
Description DZY家的后院有一块地,由N行M列的方格组成,格子内种的菜有一定的价值,并且每一条单位长度的格线有一定的费用. DZY喜欢在地里散步.他总是从任意一个格点出发,沿着格线行走直到 ...
- linux查找文件目录及mysql卸载
我们要卸载 mysql但是不知道其安装在哪里了,可以用 where +关键词 的方式查找,如上图 输入 whereis mysql 后,下面显示出了4个包含mysql的位置. ..... 查看安装m ...
- ACE反应器(Reactor)模式(4)
转载于:http://www.cnblogs.com/TianFang/archive/2006/12/18/596012.html 定时器的实现 通过Reactor机制,还可以很容易的实现定时器的功 ...
- zTab layui多标签页组件
zTab zTab是一个layui多标签页插件,仿照了layuiAdmin的iframe版Tab实现 当前版本v1.0 码云地址:https://gitee.com/sushengbuyu/zTab ...
- JAVA List集合转Page(分页对象)
/** * @version 1.0 * @author: fwjia */ import java.util.List; public class PageModel<T> { /*** ...
- MongoDB入门(3)- MongoDB备份与恢复
1. 备份 MongoDB提供了备份工具,mongodump.exe,在bin目录下,其用法如下: mongodump.exe -h localhost -d database_name -o d:\ ...
- 51Nod 最大子矩阵和 | DP
Input 第1行:M和N,中间用空格隔开(2 <= M,N <= 500). 第2 - N + 1行:矩阵中的元素,每行M个数,中间用空格隔开.(-10^9 <= M[i] < ...
- 数据结构&图论:图
在这里对图的存储和遍历进行一个规范,为以后更复杂的数据结构学习打下基础 首先是邻接矩阵的形式,适合于存稠密图,如果是全连接图就再合适不过了 int a[maxn][maxn]; 一个二维数组就可以搞定 ...
- mysql创建用户,并授权
1.创建用户 CREATE USER 'username'@'host' IDENTIFIED BY 'password'; host分下列3种情况:'%' - 所有情况都能访问‘localhost’ ...
- Spring Security 集成CAS实现单点登录
参考:http://elim.iteye.com/blog/2270446 众所周知,Cas是对单点登录的一种实现.本文假设读者已经了解了Cas的原理及其使用,这些内容在本文将不会讨论.Cas有Ser ...