← Home

260. Single Number III

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
bit manipulationC++Markdown
260

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 260. Single Number III, the solution in this repository is mainly a bit manipulation solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: bit manipulation.

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are singleNumber.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • A set is doing the membership or uniqueness work, which keeps the main loop readable.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 40 ms, faster than 7.36% of C++ online submissions for Single Number III.
02//Memory Usage: 12.3 MB, less than 5.00% of C++ online submissions for Single Number III.
03class Solution {
04public:
05    vector<int> singleNumber(vector<int>& nums) {
06        set<int> fset;
07        set<int> sset;
08        set<int> rset;
09        for(int num : nums){
10            if(fset.find(num)==fset.end()){
11                fset.insert(num);
12            }else{
13                sset.insert(num);
14            }
15        }
16        vector<int> ans(fset.size());
17        auto v_it = set_difference(fset.begin(), fset.end(), sset.begin(), sset.end(), ans.begin());
18        ans.resize(v_it-ans.begin());
19        return ans;
20    }
21};
22
23//bit manipulation
24//https://leetcode.com/problems/single-number-iii/discuss/68900/Accepted-C%2B%2BJava-O(n)-time-O(1)-space-Easy-Solution-with-Detail-Explanations
25//Runtime: 20 ms, faster than 59.77% of C++ online submissions for Single Number III.
26//Memory Usage: 10.1 MB, less than 54.17% of C++ online submissions for Single Number III.
27class Solution {
28public:
29    vector<int> singleNumber(vector<int>& nums) {
30        //xor all the numbers
31        int diff = accumulate(nums.begin(), nums.end(), 0, bit_xor<int>());
32        //get least significant bit
33        diff &= ~(diff-1);
34        //another way
35        //diff &= -diff;
36        
37        vector<int> ans(2, 0);
38        
39        for(int& num : nums){
40            if(num & diff){
41                //group 1: set at "diff"'s bit
42                ans[0] ^= num;
43            }else{
44                //group 2: not set at "diff"'s bit
45                ans[1] ^= num;
46            }
47        }
48        
49        return ans;
50    }
51};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.