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. 全球域名服务商域名增量TOP10:中国占据3个席位
  2. 大前端时代,从前端小工到架构师的进阶锦囊!
  3. (Call Stack)
  4. 图解HTTP笔记(二)——HTTP状态码
  5. 第三个维度:z-index
  6. 6. URL (2)
  7. 使用成捷讯软件制作简单的通信工程预算----以高速4G新建站为例
  8. 软件工程--螺旋模型详解
  9. win10浏览器闪退_win10系统打开ie11浏览器出现闪退的两种解决方法
  10. 新版万能声卡驱动-VoodooHDA-2.8.5
  11. 远程桌面连接命令mstsc怎么用?如何使用mstsc进行远程登录?
  12. rrd java_golang 使用rrd的相关资料
  13. p=p-next 表达了什么意思
  14. 利用Freemarker模板生成doc或者docx文档(转载整理)
  15. 软件下载传送门(个人习惯自用记录)
  16. linux挂载u盘出错_linux挂载U盘、移动硬盘后无法更改权限
  17. BGP——基本概念4(路由引入、防环、路由通告原则、路由选路)
  18. 介绍一个C++的ORM工具ODB(一) | C瓜哥的博客
  19. 20182316胡泊 第2,3周学习总结
  20. Python的enumerate()函数——浅显易懂

热门文章

  1. python pytest测试框架(一)
  2. IIC 驱动OLED
  3. 腾讯开放平台 iOS应用URL schema、Bundle ID填写 (含微博、微信)
  4. Total Bummer:透​​视水滴
  5. 创建C++字符串最常用的五种方法,难吗?有手就行!
  6. 事故频发,特斯拉还要“补课”
  7. OPEN3D学习笔记(六)——Multiway registration
  8. 复现NC图表:相关性分析气泡图(热图)---同时展示正负调控关系和显著性
  9. phpexcel的读取和导出
  10. 中文puppy linux7.5,小型Linux发行版Puppy Linux 7.5发布下载