-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathleetcode136_single_number.cpp
54 lines (44 loc) · 1.07 KB
/
leetcode136_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
/**
* @file leetcode136_single_number.cpp
* @author wangguibao(https://github.com/wangguibao)
* @date 2023/06/14 19:27
* @brief https://leetcode.com/problems/single-number/
**/
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
class Solution {
public:
int singleNumber(vector<int>& nums) {
size_t len = nums.size();
int ret = 0;
for (size_t i = 0; i < len; ++i) {
ret ^= nums[i];
}
return ret;
}
};
int main() {
int n;
std::vector<int> nums;
while (1) {
n = 0;
std::cout << "Number of elements: ";
std::cin >> n;
if (n <= 0) {
return 0;
}
std::cout << n << " integers: " << std::endl;
nums.clear();
int ele;
for (int i = 0; i < n; ++i) {
std::cin >> ele;
nums.push_back(ele);
}
Solution solution;
auto ret = solution.singleNumber(nums);
std::cout << ret << std::endl;
}
return 0;
}