作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/single-number-iii/description/

题目描述

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

For example:

Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].

Note:

  1. The order of the result is not important. So in the above example, [5, 3] is also correct.
  2. Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?

题目大意

一个数组中,有些元素出现了两次,有两个元素各自只出现1次,找出这两个元素。

解题方法

异或

想到这个系列的第一个题,就是找出单个的只出现一次的字符。做法是异或操作。这个题也是用异或。

把所有的数字进行一次异或,得到的是只出现了一次的两个数字的异或。

这两个数字不等,因此他们的二进制必定至少1位不同,即异或结果中为1的那位(一个数字的该位为1,另个数字的该位为0)。找出从右向左的第一个不同的位置(异或值为1的位置),给mask在该位置设置成1,mask的其余位置是0. mask存在的意义在于我们能通过该位置来分辨出两个只出现了一次的数字。

然后技巧性的来了:再进行一次异或操作。

每个数字都跟mask相与。通过与的结果为0和为1,即可区分出两个数字。

我刚开始有点不明白的是,为什么把所有的元素都重新异或了?其实,因为除了这两个元素以外,其他的元素都出现了两次,这两次相同的数字的和mask的与操作的结果是相同的,所以会被异或两次抵消掉。

一言以蔽之,先通过异或找出两个元素的异或结果。再根据异或结果的出现为1的位置作为mask,mask的二进制只有1位是1,也就是只看两个元素的该位置。最后,通过与操作判断该位置是0还是1区分两个元素。

参考:https://segmentfault.com/a/1190000004886431

代码:

class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
xor = 0
num1, num2 = 0, 0
for num in nums:
xor ^= num
mask = 1
while xor & mask == 0:
mask = mask << 1
for num in nums:
if num & mask == 0:
num1 ^= num
else:
num2 ^= num
return [num1, num2]

C++代码如下:

class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
int res = 0;
for (int n : nums) res ^= n;
int a = 0, b = 0;
int mask = 1;
while ((mask & res) == 0) mask <<= 1;
for (int n : nums) {
if (n & mask)
a ^= n;
else
b ^= n;
}
return {a, b};
}
};

字典

使用字典直接求只出现一次的数字即可。

class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
count = collections.Counter(nums)
res = []
for num, c in count.items():
if c == 1:
res.append(num)
return res

C++代码如下:

class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
map<int, int> count;
for (int n : nums) count[n] ++;
vector<int> res;
for (auto p : count)
if (p.second == 1)
res.push_back(p.first);
return res;
}
};

日期

2018 年 3 月 3 日
2018 年 11 月 24 日 —— 周六快乐
2018 年 12 月 10 日 —— 又是周一!

【LeetCode】260. Single Number III 解题报告(Python & C++)的更多相关文章

  1. LeetCode 260. Single Number III(只出现一次的数字 III)

    LeetCode 260. Single Number III(只出现一次的数字 III)

  2. [LeetCode] 260. Single Number III 单独数 III

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

  3. [LeetCode#260]Single Number III

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

  4. LeetCode - 136. Single Number - ( C++ ) - 解题报告 - 位运算思路 xor

    1.题目大意 Given an array of integers, every element appears twice except for one. Find that single one. ...

  5. Java [Leetcode 260]Single Number III

    题目描述: Given an array of numbers nums, in which exactly two elements appear only once and all the oth ...

  6. LeetCode 260 Single Number III 数组中除了两个数外,其他的数都出现了两次,找出这两个只出现一次的数

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

  7. Leetcode 260 Single Number III 亦或

    在一个数组中找出两个不同的仅出现一次的数(其他数字出现两次) 同样用亦或来解决(参考编程之美的1.5) 先去取出总亦或值 然后分类,在最后一位出现1的数位上分类成 ans[0]和ans[1] a&am ...

  8. [LeetCode] 260. Single Number III(位操作)

    传送门 Description Given an array of numbers nums, in which exactly two elements appear only once and a ...

  9. leetcode 136 Single Number, 260 Single Number III

    leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...

随机推荐

  1. SourceTree git 工作流

    转载自:https://www.cnblogs.com/tian-xie/p/6264104.html 1. SourceTree是什么 拥有可视化界面的项目版本控制软件,适用于git项目管理 win ...

  2. 日常Java 2021/10/18

    Vecter类实现了一个动态数组,不同于ArrayList的是,Vecter是同步访问的, Vecter主要用在事先不知道数组的大小或可以改变大小的数组 Vecter类支持多种构造方法:Vecter( ...

  3. flink-----实时项目---day05-------1. ProcessFunction 2. apply对窗口进行全量聚合 3使用aggregate方法实现增量聚合 4.使用ProcessFunction结合定时器实现排序

    1. ProcessFunction ProcessFunction是一个低级的流处理操作,可以访问所有(非循环)流应用程序的基本构建块: event(流元素) state(容错,一致性,只能在Key ...

  4. 【STM8】添加头文件、加入库函数

    下面顺便放上STM8L15x-16x-05x的固件库,以及固件库里没有的<stm8l15x_conf.h> 链接打开后,还会发现另外两个文件夹,<src><inc> ...

  5. Gradle—Android配置详解

    参考[1]彻底弄明白Gradle相关配置       [2]Android Studio gradle配置详解

  6. 利用unordered_map维护关联数据

    在leetcode上刷339题Evaluate Division(https://leetcode.com/problems/evaluate-division/#/description)时在脑中过 ...

  7. 队列——Java实现

    1 package struct; 2 3 interface IQueue{ 4 //入队列 5 void add(Object obj); 6 //出队列 7 Object remove(); 8 ...

  8. Linux基础命令---echo打印内容到标准输出

    echo echo指令可以输出内容到标准输出,以空白分割字符串,并且后面增加换行. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora.   1.语法       ec ...

  9. GO 时间处理

    比较大小 比较大小 先把当前时间格式化成相同格式的字符串,然后使用time的Before, After, Equal 方法即可. time1 := "2015-03-20 08:50:29& ...

  10. clickhouse安装数据导入及查询测试

    官网 https://clickhouse.tech/ quick start ubantu wget https://repo.yandex.ru/clickhouse/deb/lts/main/c ...