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. HashMap & SparseArray & ArrayMap 简单说明

    HashMap 使用有限一维拉链数组存储结构,鉴于所用Entry结构{key, value, nextExtry},Key的hash值用于取余获得所属的数组行下标,通过链表方式顺序存放所有余数相同的各 ...

  2. C#学习目录处理

    目录获取和处理: string path = ".";//表明要在当前所在的目录 //先定义目录信息变量 DirectoryInfo dir = new DirectoryInfo ...

  3. POJ 2976 二分

    Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12515   Accepted: 4387 D ...

  4. React Mixin

    为什么使用 Mixin ? React为了将同样的功能添加到多个组件当中,你需要将这些通用的功能包装成一个mixin,然后导入到你的模块中. 可以说,相比继承而已,React更喜欢这种组合的方式. 写 ...

  5. 耗子学Python了(1)___Python环境配置

    一:写在开始的开始 最近我们团对的老大问我想看什么书,我让老大买了一本<Python 3面向对象编程>,所以在看的时候边敲边实践,决定在自己电脑配置下相应的环境,然后开始Python的学习 ...

  6. 洛谷P2860 [USACO06JAN]冗余路径Redundant Paths

    题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1. ...

  7. POj 2104 K-th Number (分桶法+线段树)

    题目链接 Description You are working for Macrohard company in data structures department. After failing ...

  8. Warning: File upload error - unable to create a temporary file in Unknown on line 0

    upload_tmp_dir 临时文件夹问题 上传文件提示 Warning: File upload error - unable to create a temporary file in Unkn ...

  9. hdu 1159 Common Subsequence(最长公共子序列 DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  10. 在linux程序里面,知道一个函数地址,改函数是属于某个动态库的,怎么样得到这个动态库的全【转】

    转自:http://www.360doc.com/content/17/1012/11/48326749_694292472.shtml 另外dl_iterate_phdr可以查到当前进程所装在的所有 ...