# LeetCode 260. 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.

Example:

Input: [1,2,1,3,2,5]

Output: [3,5]

Note:

The order of the result is not important. So in the above example, [5, 3] is also correct.

Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?

## 实现

# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2019-02-03 09:02:18
class Solution:
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
res = [0, 0]
diff = 0
for num in nums:
diff ^= num
# 制造分离因子
diff = diff & (~(diff - 1))
for num in nums:
if (num & diff) == 0:
res[0] ^= num
else:
res[1] ^= num
return res

|
6月前
|

Leetcode 313. Super Ugly Number

66 1
|
11天前
|

【LeetCode力扣】单调栈解决Next Greater Number（下一个更大值）问题
【LeetCode力扣】单调栈解决Next Greater Number（下一个更大值）问题
8 0
|
6月前
|

Leetcode Single Number II （面试题推荐）

22 0
|

LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
93 0
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
67 0
|

LeetCode 136. 只出现一次的数字 Single Number
LeetCode 136. 只出现一次的数字 Single Number
48 0
|
2天前
|

7 1
|
2天前
|

JAVA数据结构刷题 -- 力扣二叉树
JAVA数据结构刷题 -- 力扣二叉树
8 0
|
11天前
|

【刷题】Leetcode 1609.奇偶树

12 0
|
11天前
|

【刷题】滑动窗口精通 — Leetcode 30. 串联所有单词的子串 | Leetcode 76. 最小覆盖子串

16 0