[Easy] 169. Majority Element
169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
You may assume that the array is non-empty and the majority element always exist in the array.
Example 1:
Input: [3,2,3]
Output: 3
Example 2:
Input: [2,2,1,1,1,2,2]
Output: 2
Solution
Hash Table
Runtime: 48 ms, faster than 44.37% of C++ online submissions for Majority Element.
Memory Usage: 20 MB, less than 6.06% of C++ online submissions for Majority Element.
时间、空间复杂度O(n)
class Solution {public:int majorityElement(vector<int>& nums) {unordered_map<int, int> map;for(int i = 0; i < nums.size(); i++){map[nums[i]]++;}for(auto it = map.begin(); it != map.end(); ++it){if(it->second > nums.size()/2) return it->first;}return 0;}
};
Sorting
Runtime: 44 ms, faster than 59.04% of C++ online submissions for Majority Element.
Memory Usage: 19.6 MB, less than 6.06% of C++ online submissions for Majority Element.
class Solution {public:int majorityElement(vector<int>& nums) {nth_element(nums.begin(), nums.begin() + nums.size() / 2, nums.end());return nums[nums.size() / 2];}
};
void nth_element (RandomAccessIterator first, RandomAccessIterator nth,RandomAccessIterator last);
Rearranges the elements in the range [first,last),
in such a way that the element at the nth position is the element that would be in that position in a sorted sequence.nth_dement() 的执行会导致第 n 个元素被放置在适当的位置。
这个范围内,在第 n 个元素之前的元素都小于第 n 个元素,而且它后面的每个元素都会比它大。
由于是用来找众数,所以不必全排序。
平均时间复杂度O(n)
[Easy] 169. Majority Element相关推荐
- LeetCode - Easy - 169. Majority Element
Topic Array Divide and Conquer Bit Manipulation Description https://leetcode.com/problems/majority-e ...
- leetcode讲解--169. Majority Element
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- [勇者闯LeetCode] 169. Majority Element
[勇者闯LeetCode] 169. Majority Element Description Given an array of size n, find the majority element. ...
- Leet Code OJ 169. Majority Element [Difficulty: Easy]
题目: Given an array of size n, find the majority element. The majority element is the element that ap ...
- leetcode 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【LeetCode】169. Majority Element
原题链接:https://leetcode.com/problems/majority-element/description/ 要求: Given an array of size n, find ...
- Leetcode - 169. Majority Element (多数投票问题)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【LeetCode从零单排】No.169 Majority Element(hashmap用法)
题目 Given an array of size n, find the majority element. The majority element is the element that app ...
- [LeetCode]: 169: Majority Element
题目: Given an array of size n, find the majority element. The majority element is the element that ap ...
最新文章
- 脑科学与脑电基础知识汇总
- 收集Oracle常用命令----索引及约束
- STM32外设有哪些?外设在总线上是怎么挂载的?
- Windows10彻底禁止更新
- 这五个网站可以让你免费学习编程,快收藏!
- 在使用Asp.net制作网站的时候遇到的问题(二)
- VC如何在单文档里显示对话框
- C# ConcurrentBag的实现原理
- line-height:2和line-height:2em的区别,它们是有区别的
- html5缓动下拉菜单,HTML5 Canvas鼠标跟随的缓动效果
- 拖拽之路(二):自定义QListWidget实现美观的拖拽样式(拖拽不影响选中)
- 011—MySQL 事务
- c语言视频教程全集(c语言视频教程 谭浩强)
- 布客·ApacheCN 翻译/校对/笔记整理活动进度公告 2020.1
- 自己动手写2D物理引擎-初级篇(1)
- 如何远程登陆Linux服务器
- Xdebug中文文档-堆栈跟踪
- Win11 专业工作站版安装安卓子系统方法 (离线包安装)
- 软件测试之构建测试---BVT
- 2021年山东省青少年人工智能培训
热门文章
- 看完这些面试必问的Linux小知识,我保证你面试后会来给我的文章一键三连
- 2020我们都用这3个3D建模软件,常用简单又易上手!
- 异常检测——Anomaly Detection
- 音频有噪音怎么办?不如试试这款软件。
- python基础语言与应用第五章_Python基础教程读书笔记(第5章—第6章:条件、循环和其他语句;抽象)...
- python api调用百度ai平台_百度ai开放平台使用方法(附带详细案例步骤)
- oracle 12cR2 版本发布时间 2HCY2016
- error @typescript-eslint/eslint-plugin@5.3.0: The engine “node“ is incompatible with this module.
- 诊断和响应故障_执行闪回和数据库时间点恢复(Flashback and Database Point-in-Time Recovery)
- css亮黄色_写给前端工程师的色彩常识:色彩三属性及其在CSS中的应用