add: 220221 [cpp]
This commit is contained in:
parent
cf70fcd0dd
commit
5c8f652039
|
|
@ -0,0 +1,24 @@
|
|||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
/**
|
||||
* 169. Majority Element
|
||||
* Given an array nums of size n, return the majority element.
|
||||
* The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.
|
||||
*/
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
static inline int majorityElement(const std::vector<int>& nums) {
|
||||
int n = nums.size() >> 1;
|
||||
std::unordered_map<int, int> m;
|
||||
for (int i : nums)
|
||||
if (++m[i] > n)
|
||||
return i;
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -3,4 +3,4 @@ PROJECT(2202)
|
|||
|
||||
SET(CMAKE_CXX_STANDARD 23)
|
||||
|
||||
ADD_EXECUTABLE(2202 220221-CN.cpp)
|
||||
ADD_EXECUTABLE(2202 220221.cpp)
|
||||
|
|
|
|||
Loading…
Reference in New Issue