-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleNumber.cpp
More file actions
46 lines (38 loc) · 986 Bytes
/
Copy pathsingleNumber.cpp
File metadata and controls
46 lines (38 loc) · 986 Bytes
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
// Source : https://leetcode.com/problems/single-number/
// Author : https://github.com/xinsheng
// Date : 2021-12-08
/*
Discuss:
https://leetcode.com/problems/single-number/discuss/1617742/C%2B%2B-easy-solution
*/
#include <string.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
int singleNumber(vector<int>& nums) {
if (nums.size() == 1) return nums[0];
sort(nums.begin(), nums.end());
if(nums[0] != nums[1]) return nums[0];
int n = nums.size();
for (int i = 1; i <n-1; i++) {
if (nums[i - 1] != nums[i] && nums[i] != nums[i + 1])
return nums[i];
}
return nums[n-1];
}
};
class Solution_2 {
public:
int singleNumber(vector<int>& nums) {
int res = nums[0];
for (int i=1; i<nums.size();i++)
{
//Apply XOR operation
res ^= nums[i];
}
return res;
}
};