LeetCode——single-number系列

Question 1

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?

Solution

这个题的求解用到了按位异或的操作,因为两个相同的数异或是为0的,然后按位操作又满足交换律,所以一直异或下去就能得到单独的一个数字,这有点像模拟二进制求和。

class Solution {
public:
int singleNumber(int A[], int n) { int sum = 0;
for (int i = 0; i < n; i++) {
sum ^= A[i];
}
return sum;
}
};

进阶版本

Question 2

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?

Solution

要是如果有三进制的操作,那么这道题就很简单了,那么我们需要模拟三进制的操作,ones表示一个1的个数,twos表示两个1的个数,ones&twos结果表示三个1的个数。

class Solution {
public:
int singleNumber(int A[], int n) {
int ones = 0;
int twos = 0;
int threes;
for (int i = 0; i < n; i++) {
int t = A[i];
twos |= ones & t; // 或表示加,与表示求两个1的个数
ones ^= t; // 异或操作表示统计一个1的个数
threes = ones & twos; // 与表示三个1的个数
ones &= ~threes;
twos &= ~threes;
}
return ones;
}
};

还有一种用额外存储空间的解法,统计每一位1的个数。

class Solution {
public:
int singleNumber(int A[], int n) {
int bitSum[32] = {0};
for (int i = 0; i < n; i++) {
int mask = 0x1;
for (int j = 0; j < 32; j++) {
int bit = A[i] & mask;
if (bit != 0)
bitSum[j] += 1;
mask = mask << 1;
}
}
int res = 0;
for (int i = 31; i >= 0; i--) {
res = res << 1;
res += (bitSum[i] % 3);
}
return res;
}
};

LeetCode——single-number系列的更多相关文章

  1. leetcode single number系列

    这个系列一共有三题,第一题是一组数里除了一个数出现一次之外,其他数都是成对出现,求这个数. 第二题是一组数里除了两个数出现一次外,其他数都是成对出现,求这两个数 第三题是一组数里除了一个数出现一次外, ...

  2. [LeetCode] Single Number III 单独的数字之三

    Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...

  3. [LeetCode] Single Number II 单独的数字之二

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  4. [LeetCode] Single Number 单独的数字

    Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...

  5. LeetCode Single Number I / II / III

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

  6. LeetCode:Single Number II

    题目地址:here 题目大意:一个整数数组中,只有一个数出现一次,其余数都出现3次,在O(n)时间,O(1)空间内找到这个出现一次的数 对于”只有一个数出现一次,其余数出现2次“的情况,很简单,只要把 ...

  7. LeetCode Single Number III

    原题链接在这里:https://leetcode.com/problems/single-number-iii/ 题目: Given an array of numbers nums, in whic ...

  8. [leetcode]Single Number II @ Python

    原题地址:http://oj.leetcode.com/problems/single-number-ii/ 题意:Given an array of integers, every element ...

  9. LeetCode——Single Number II(找出数组中只出现一次的数2)

    问题: Given an array of integers, every element appears three times except for one. Find that single o ...

  10. LeetCode: Single Number I && II

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

随机推荐

  1. EditText ------- 键盘类型

    文本输入框指定软键盘类型和软键盘回车键图标设置, 转载:http://blog.csdn.net/wirelessqa/article/details/8567327

  2. AndroidManifest.xml文件详解(activity)(五)

    android:taskAffinity 这个属性用于跟Activity有亲缘关系的任务.带有相同亲缘关系的Activity,在概念上是属于相同任务的(从用户的角度看,它们是属于同一应用程序的).任务 ...

  3. 【BZOJ3039】玉蟾宫 单调栈

    [BZOJ3039]玉蟾宫 Description 有一天,小猫rainbow和freda来到了湘西张家界的天门山玉蟾宫,玉蟾宫宫主蓝兔盛情地款待了它们,并赐予它们一片土地.这片土地被分成N*M个格子 ...

  4. resolution will not be reattempted until the update interval of vas has elap

    转自:http://kia126.iteye.com/blog/1785120 maven在执行过程中抛错: 引用 ... was cached in the local repository, re ...

  5. @Override must override a superclass method 有关问题解决

    1.Java开发环境时 如果在使用Eclipse开发Java项目时,在使用 @Override 出现以下错误: The method *** of type *** must override a s ...

  6. [LintCode] 尾部的零

    class Solution { public: // param n : description of n // return: description of return long long tr ...

  7. 下载flv格式视频

    我们再看flash plaer播放视频时,有些时候需要下载,无奈找不到下载的按钮.这时,我们可以用以下的方式来进行下载. 其它格式估计也是有迹可循,大家仔细看看网页源代码,看到类似于这种地址,看到有相 ...

  8. Centos6.5下DHCP服务器的安装和配置

    1.首先需要安装DHCP的软件包,使用yum进行安装 # yum install -y dhcp.x86_64  dhcp-devel.x86_64 2.将/usr/share/doc/dhcp-4. ...

  9. 第三课作业——set类型、sorted set类型的增删改查,redis的事务

    第三课时作业 静哥 by 2016.2.23~2016.3.6   [作业描述] 1.总结什么是set以及什么是sorted set,并完成对set以及sorted set的增删改查(查需要至少4种方 ...

  10. Linux的.a、.so和.o文件 windows下obj,lib,dll,exe的关系

    Linux的.a..so和.o文件 - chlele0105的专栏 - CSDN博客 https://blog.csdn.net/chlele0105/article/details/23691147 ...