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相关推荐

  1. LeetCode - Easy - 169. Majority Element

    Topic Array Divide and Conquer Bit Manipulation Description https://leetcode.com/problems/majority-e ...

  2. leetcode讲解--169. Majority Element

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  3. [勇者闯LeetCode] 169. Majority Element

    [勇者闯LeetCode] 169. Majority Element Description Given an array of size n, find the majority element. ...

  4. 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 ...

  5. leetcode 169. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  6. 【LeetCode】169. Majority Element

    原题链接:https://leetcode.com/problems/majority-element/description/ 要求: Given an array of size n, find ...

  7. Leetcode - 169. Majority Element (多数投票问题)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  8. 【LeetCode从零单排】No.169 Majority Element(hashmap用法)

    题目 Given an array of size n, find the majority element. The majority element is the element that app ...

  9. [LeetCode]: 169: Majority Element

    题目: Given an array of size n, find the majority element. The majority element is the element that ap ...

最新文章

  1. 脑科学与脑电基础知识汇总
  2. 收集Oracle常用命令----索引及约束
  3. STM32外设有哪些?外设在总线上是怎么挂载的?
  4. Windows10彻底禁止更新
  5. 这五个网站可以让你免费学习编程,快收藏!
  6. 在使用Asp.net制作网站的时候遇到的问题(二)
  7. VC如何在单文档里显示对话框
  8. C# ConcurrentBag的实现原理
  9. line-height:2和line-height:2em的区别,它们是有区别的
  10. html5缓动下拉菜单,HTML5 Canvas鼠标跟随的缓动效果
  11. 拖拽之路(二):自定义QListWidget实现美观的拖拽样式(拖拽不影响选中)
  12. 011—MySQL 事务
  13. c语言视频教程全集(c语言视频教程 谭浩强)
  14. 布客·ApacheCN 翻译/校对/笔记整理活动进度公告 2020.1
  15. 自己动手写2D物理引擎-初级篇(1)
  16. 如何远程登陆Linux服务器
  17. Xdebug中文文档-堆栈跟踪
  18. Win11 专业工作站版安装安卓子系统方法 (离线包安装)
  19. 软件测试之构建测试---BVT
  20. 2021年山东省青少年人工智能培训

热门文章

  1. 看完这些面试必问的Linux小知识,我保证你面试后会来给我的文章一键三连
  2. 2020我们都用这3个3D建模软件,常用简单又易上手!
  3. 异常检测——Anomaly Detection
  4. 音频有噪音怎么办?不如试试这款软件。
  5. python基础语言与应用第五章_Python基础教程读书笔记(第5章—第6章:条件、循环和其他语句;抽象)...
  6. python api调用百度ai平台_百度ai开放平台使用方法(附带详细案例步骤)
  7. oracle 12cR2 版本发布时间 2HCY2016
  8. error @typescript-eslint/eslint-plugin@5.3.0: The engine “node“ is incompatible with this module.
  9. 诊断和响应故障_执行闪回和数据库时间点恢复(Flashback and Database Point-in-Time Recovery)
  10. css亮黄色_写给前端工程师的色彩常识:色彩三属性及其在CSS中的应用