forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main1.cpp
41 lines (30 loc) · 862 Bytes
/
main1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/// Source : https://leetcode.com/problems/single-number/
/// Author : liuyubobobo
/// Time : 2016-12-05
#include <iostream>
#include <vector>
#include <unordered_set>
#include <cassert>
using namespace std;
/// Using hashtable to find the one
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int singleNumber(vector<int>& nums) {
assert(nums.size()%2 == 1);
unordered_set<int> hashtable;
for(int i = 0 ; i < nums.size() ; i ++)
if(hashtable.find(nums[i]) == hashtable.end())
hashtable.insert(nums[i]);
else
hashtable.erase(nums[i]);
assert(hashtable.size() == 1);
return *hashtable.begin();
}
};
int main() {
vector<int> nums = {0, 0, 1, 1, 2};
cout << Solution().singleNumber(nums) << endl;
return 0;
}