add: 220215-CN
This commit is contained in:
parent
512775b7be
commit
152272dcb1
|
|
@ -0,0 +1,27 @@
|
||||||
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
#include <unordered_set>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 136. Single Number
|
||||||
|
* Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
|
||||||
|
* You must implement a solution with a linear runtime complexity and use only constant extra space.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
static int singleNumber(const std::vector<int>& nums) {
|
||||||
|
std::unordered_set<int> s;
|
||||||
|
for (int i : nums)
|
||||||
|
if (s.count(i))
|
||||||
|
s.erase(i);
|
||||||
|
else
|
||||||
|
s.insert(i);
|
||||||
|
return *s.begin();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::cout << Solution::singleNumber({4,1,2,1,2});
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
@ -3,4 +3,4 @@ PROJECT(2202)
|
||||||
|
|
||||||
SET(CMAKE_CXX_STANDARD 23)
|
SET(CMAKE_CXX_STANDARD 23)
|
||||||
|
|
||||||
ADD_EXECUTABLE(2202 220215-CN.cpp)
|
ADD_EXECUTABLE(2202 220215.cpp)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue