From 5c8f652039c0624daf1d70c26ce7da3890f52906 Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Tue, 22 Feb 2022 00:29:46 +0800 Subject: [PATCH] add: 220221 [cpp] --- cpp/2202/220221.cpp | 24 ++++++++++++++++++++++++ cpp/2202/CMakeLists.txt | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 cpp/2202/220221.cpp diff --git a/cpp/2202/220221.cpp b/cpp/2202/220221.cpp new file mode 100644 index 0000000..560c9eb --- /dev/null +++ b/cpp/2202/220221.cpp @@ -0,0 +1,24 @@ +#include +#include + +/** + * 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& nums) { + int n = nums.size() >> 1; + std::unordered_map m; + for (int i : nums) + if (++m[i] > n) + return i; + return 0; + } +}; + +int main() { + return 0; +} diff --git a/cpp/2202/CMakeLists.txt b/cpp/2202/CMakeLists.txt index 3decd39..0f423ba 100644 --- a/cpp/2202/CMakeLists.txt +++ b/cpp/2202/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2202) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2202 220221-CN.cpp) +ADD_EXECUTABLE(2202 220221.cpp)