-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path268_MissingNumber.cpp
More file actions
52 lines (52 loc) · 1.03 KB
/
Copy path268_MissingNumber.cpp
File metadata and controls
52 lines (52 loc) · 1.03 KB
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
#include<iostream>
#include<vector>
#include<algorithm>
#include<unordered_set>
using namespace std;
int missingNumber(vector<int>& nums) {
sort(nums.begin(), nums.end());
for (int i = 0; i < nums.size(); i++)
if (nums[i] != i)
return i;
return nums.size();
}
/*
*/
int missingNumber_1(vector<int>& nums) {
unordered_set<int> container;
for (int i : nums) {
container.insert(i);
}
for (int i = 0; i < nums.size()+1; i++)
if (!container.count(i))
return i;
}
/*
利用异或运算,
Index 0 1 2 3
Value 0 1 3 4
missing =4∧(0∧0)∧(1∧1)∧(2∧3)∧(3∧4)
=(4∧4)∧(0∧0)∧(1∧1)∧(3∧3)∧2
=0∧0∧0∧0∧2
=2
4为向量长度
*/
int missingNumber_2(vector<int>& nums) {
int t = nums.size();
for (int i = 0; i < nums.size(); i++) {
t ^= i ^ nums[i];
}
return t;
}
/*
利用高斯公式
*/
int missingNumber_3(vector<int>& nums) {
int sum = nums.size() * (nums.size() + 1) / 2;
int temp = 0;
for (int i : nums) {
temp += i;
}
return sum-temp;
}