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. ASP.NET MVC 数据库依赖缓存

    ASP.NET MVC 数据库依赖缓存   问题背景 最近做一个非常简单的功能,就是使用ajax请求的方式从服务端请求一段下拉表的数据. 以前也有做过这个功能,只不过这次做这个功能的时候冒出了一个想法 ...

  2. git pull报错:There is no tracking information for the current branch

    报错: There is no tracking information for the current branch. Please specify which branch you want to ...

  3. CodeFores 665D Simple Subset(贪心)

    D. Simple Subset time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  4. 巨蟒python全栈开发flask3

    首先,我们新建一个项目: 这个时候,我们调用ab函数,可以在所有的模板中使用. 上边是一个特殊装饰器, 1.flask特殊装饰器 下面说几个特殊的装饰器 再请求之前的装饰器 运行: 这个时候,服务端打 ...

  5. Java Json API:Gson使用简单入门

    GSON是Google开发的Java API,用于转换Java对象和Json对象.本文讨论并提供了使用API的简单代码示例.更多关于GSON的API可以访问:http://sites.google.c ...

  6. 改变label中的某字体颜色

    NSString *allString=@"你家在哪里,需要我送你么"; NSString *subString=@"在哪里"; UILabel *string ...

  7. 3.php数据类型中NULL,"",0的比较

    <?php //赋值 $some1 = NULL; $some2 = 0; $some3 = ""; //0与NULL比较 echo $some1==$some2; echo ...

  8. simplest_ffmpeg_grabdesktop:屏幕录制。 simplest_ffmpeg_readcamera:读取摄像头

    最简单的基于FFmpeg的AVDevice例子(屏幕录制) - 雷霄骅(leixiaohua1020)的专栏 - CSDN博客 https://blog.csdn.net/leixiaohua1020 ...

  9. Java基础语法 - 面向对象 - this 关键字

    在Java语言中规定使用this关键字来代表本类对象的引用,this关键字被隐式地用于引用对象的成员变量和方法. this关键字引用的就是本类的一个对象,在局部变量或方法参数覆盖了成员变量时,就要添加 ...

  10. SOE 中调用第三方dll

    一.简介 在利用soe实现server的扩展的时候,有些时候,需要调用第三方的dll库.官网中给出了明确的说明,soe中是可以添加第三方的dll文件,但是一直没有测试.按照官方的步骤应该是一个非常的简 ...