本文提供了一种使用C++解决LeetCode 260 Single Number III问题的方法,包括两种解法:一种是利用set容器,另一种是采用异或位运算符实现线性时间和常数空间复杂度的解决方案。
LeetCode 260. Single Number III 题解(C++)
题目描述
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.
示例
Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].
补充
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?