【Leetcode 260】Single NumberIII
难度: 中等(Medium)
题目
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].
思路
所有数字的异或值就是那两个数字的异或值,设这个值的最右端的1为第i位,那么根据第i位是否为1可将所有数字分为两类,对每一类再进行一次异或就得到这两个值。更详细的内容可参考<<剑指offer>>
代码
1 | class Solution(object): |
评论系统未开启,无法评论!