-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy path136. Single Number.cpp
140 lines (109 loc) · 3.12 KB
/
136. Single Number.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
Given a non-empty array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,1]
Output: 1
Example 2:
Input: [4,1,2,1,2]
Output: 4
**/
//Runtime: 36 ms, faster than 7.03% of C++ online submissions for Single Number.
//Memory Usage: 11.9 MB, less than 5.09% of C++ online submissions for Single Number.
class Solution {
public:
int singleNumber(vector<int>& nums) {
map<int, int> mymap;
for(int num : nums){
if(mymap.find(num)==mymap.end()){
mymap[num] = 1;
}else{
mymap[num] += 1;
}
}
for(map<int, int>::iterator it=mymap.begin(); it!=mymap.end(); it++){
if(it->second==1) return it->first;
}
return -1;
}
};
//using set
//Runtime: 32 ms, faster than 10.91% of C++ online submissions for Single Number.
//Memory Usage: 11.8 MB, less than 5.09% of C++ online submissions for Single Number.
/**
class Solution {
public:
int singleNumber(vector<int>& nums) {
set<int> myset;
for(int num : nums){
if(myset.find(num)==myset.end()){
myset.insert(num);
}else{
myset.erase(num);
}
}
if(!myset.empty()){
return *myset.begin();
}
return -1;
}
};
**/
//math
//2∗(a+b+c)−(a+a+b+b+c)=c
/**
Complexity Analysis
Time complexity : O(n + n) = O(n). sum will call next to iterate through nums.
We can see it as sum(list(i, for i in nums)) which means the time complexity is O(n)
because of the number of elements(n) in nums.
Space complexity : O(n + n) = O(n). set needs space for the elements in nums **/
//Runtime: 36 ms, faster than 7.05% of C++ online submissions for Single Number.
//Memory Usage: 11.8 MB, less than 5.09% of C++ online submissions for Single Number.
/**
class Solution {
public:
int singleNumber(vector<int>& nums) {
set<int> myset (nums.begin(), nums.end());
int ans = 0;
for(int e : myset){
ans += e*2;
}
for(int e : nums){
ans -= e;
}
return ans;
}
};
**/
/**
Approach 4: Bit Manipulation
Concept
If we take XOR of zero and some bit, it will return that bit
a⊕0=a
If we take XOR of two same bits, it will return 0
a⊕a=0
a⊕b⊕a=(a⊕a)⊕b=0⊕b=b
So we can XOR all bits together to find the unique number.
**/
/**
Complexity Analysis
Time complexity : O(n).
We only iterate through nums,
so the time complexity is the number of elements in nums.
Space complexity : O(1).
**/
//Runtime: 16 ms, faster than 96.66% of C++ online submissions for Single Number.
//Memory Usage: 9.6 MB, less than 92.26% of C++ online submissions for Single Number.
/**
class Solution {
public:
int singleNumber(vector<int>& nums) {
int ans=0;
for(int num : nums){
ans^=num;
}
return ans;
}
};
**/