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——还没看,倒过头来再看的更多相关文章

  1. 4.Single Number && Single Number (II)

    Single Number: 1. Given an array of integers, every element appears twice except for one. Find that ...

  2. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

  3. LeetCode Single Number I II Python

    Single Number Given an array of integers, every element appears twice except for one. Find that sing ...

  4. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  5. Leetcode 137. Single Number I/II/III

    Given an array of integers, every element appears twice except for one. Find that single one. 本题利用XO ...

  6. LeetCode: Single Number I && II

    I title: Given an array of integers, every element appears twice except for one. Find that single on ...

  7. single number i && ii && iii

    Problem statement Elementary knowledge: There is a popular question when I seeked my job at Beijing: ...

  8. LeetCode 【Single Number I II III】

    Given an array of integers, every element appears twice except for one. Find that single one. 思路: 最经 ...

  9. 【深入理解JAVA虚拟机】第4部分.程序编译与代码优化.1.编译期优化。这章编译和实战部分没理解通,以后再看。

    1.概述 1.1.编译器的分类 前端编译器:Sun的Javac. Eclipse JDT中的增量式编译器(ECJ)[1].  把*.java文件转变成*.class文件 JIT编译器:HotSpot ...

随机推荐

  1. 关于JavaScript的push()函数

    push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度.返回值为把指定的值添加到数组后的新长度. 语法:arrayObject.push(newelement1,newelement2,. ...

  2. HDU3068:最长回文(Manacher模板)

    最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  3. rn初体验

    react-native 需要的工具 .nodejs .rn cli .xcode and as ---------------- 打开终端,切换到根路径(mac中修改npm的默认安装来源) 一.op ...

  4. ufw坑

    ufw就是一个iptables的快捷应用.今天被这个给坑了. 一个同时没事随便修改ufw,结果ssh登陆不上,ldap什么的都被阻断了. 直接iptables -F,结果忘了修改policy,直接没法 ...

  5. ClusterId read in ZooKeeper is null 处理

    ClusterId read in ZooKeeper is null. Re-running the program after fixing issue 1 will result in the ...

  6. idea 多模块引用

    roma-server 引用common-utils的类,所以在roma-server 的pom中配置 <dependency> <groupId>org.springfram ...

  7. Android如何在初始化的时候获取加载的布局的宽高

    在自定义ListView中,需要将下拉刷新的View在初始化的时候设置padding隐藏起来,这时就要在初始化的时候获得要加载的布局View的高度. private View headView; he ...

  8. UVA 1648 Business Center

    https://vjudge.net/problem/UVA-1648 设上升x层,列个方程解出来,再把x带回去 #include<cmath> #include<cstdio> ...

  9. SPOJ 104 HIGH - Highways

    HIGH - Highways http://www.spoj.com/problems/HIGH/ In some countries building highways takes a lot o ...

  10. 【设计模式】 模式PK:策略模式VS状态模式

    1.概述 行为类设计模式中,状态模式和策略模式是亲兄弟,两者非常相似,我们先看看两者的通用类图,把两者放在一起比较一下. 策略模式(左)和状态模式(右)的通用类图. 两个类图非常相似,都是通过Cont ...