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. NHibernate VS IbatisNet

      NHibernate 是当前最流行的 Java O/R mapping 框架Hibernate 的移植版本,当前版本是 1.0 .2 .它出身于sf.net..IbatisNet 是另外一种优秀的 ...

  2. mysql如何判断一个字符串是否包含另外一个字符串?

    转自:http://blog.csdn.net/hechurui/article/details/49278493 判断子字符串在父字符串当中的索引: SELECT LOCATE("b&qu ...

  3. CodeForces 670C Cinema(排序,离散化)

    C. Cinema time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  4. javascript中innerHTML的问题

    获取document.getElementById()时,使用innerHTML获取标签获取内容,要注意: 1.要让内容先加载完,才可以使用,不然获取的是空,使用:window.onload = .. ...

  5. 原生js实现ajax方法

    下面是一个比较完整的Ajax function ajax(){ var ajaxData = { type:arguments[0].type || "GET", url:argu ...

  6. 转!java自定义注解

    转自:http://blog.csdn.net/yixiaogang109/article/details/7328466  Java注解是附加在代码中的一些元信息,用于一些工具在编译.运行时进行解析 ...

  7. module使用

    官方文档:http://modules.sourceforge.net/ 加载 module load 卸载 module unload 查看已加载 module list 查看可用 module a ...

  8. django使用celery实现异步操作

    需求: django支持的http请求都是同步的,对于需要耗时较长的操作可能会导致阻塞.为此我们需要引入异步处理机制,即收到客户端请求后立即给予响应,具体任务交给另一个进程处理. 使用方法: 1. 安 ...

  9. tensorflow 的 tutorial 的卷积神经网络的例子 convolutional.py

    具体的网址在这里: https://github.com/tensorflow/tensorflow/tree/r0.12/tensorflow/models 一个卷积神经网络用于股票分析的例子:  ...

  10. python16_day08【异常、多线程】

    一.反射及相关 1.isinstance(obj, cls) 检查是否obj是否是类 cls 的对象 class Foo(object): pass obj = Foo() isinstance(ob ...