-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleNumber.cpp
More file actions
40 lines (35 loc) · 813 Bytes
/
singleNumber.cpp
File metadata and controls
40 lines (35 loc) · 813 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
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int singleNumber(vector<int> &nums)
{
int size = nums.size();
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
if (nums[j] > nums[j + 1])
{
int temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
for (int i = 1; i < nums.size(); i += 2)
{
if (nums[i] != nums[i - 1])
return nums[i - 1];
}
return nums[nums.size() - 1];
}
};
int main()
{
vector<int> nums{4, 1, 2, 1, 2};
Solution s;
cout << s.singleNumber(nums) << endl;
return 0;
}