###### tags: `LeetCode` `Easy` # LeetCode #169 [Majority Element](https://leetcode.com/problems/majority-element) ### (Easy) 給定一個大小為 n 的數組,找到其中的多數元素。多數元素是指在數組中出現次數 大於 ⌊ n/2 ⌋ 的元素。 你可以假設數組是非空的,並且給定的數組總是存在多數元素。 --- 由於前提提到多數元素出現次數大於 ⌊ n/2 ⌋, 故只要將數組排序後尋找中位數即可。 --- ``` class Solution { public: int majorityElement(vector<int>& nums) { sort(nums.begin(),nums.end()); return nums[(nums.size()-1)/2]; } }; ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up