【leetcode】Majority Element
来源:互联网 发布:airplayiphone连mac 编辑:程序博客网 时间:2024/06/17 20:05
Question
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.
Code
#include<iostream>#include<map>#include<vector>using namespace std;class Solution {public: int majorityElement(vector<int>& nums) { map<int, int> count; int n = nums.size(); for (int i = 0; i < n; i++) { count[nums[i]]++; } for (map<int, int>::iterator it = count.begin(); it != count.end(); it++) { if (it->second >= (n+1)/ 2) { return it->first; } } return -1; }};int main() { Solution so; vector<int> nums = {1,3,2,2}; cout << so.majorityElement(nums) << endl; system("pause"); return 0;}
0 0
- 【leetcode】Majority Element II,Majority element algorithm
- Leetcode: Majority Element &Majority Element II
- Leetcode Majority Element
- leetcode 169-Majority Element
- 【leetcode 哈希表】Majority Element
- LeetCode:Majority Element
- leetcode:Majority Element
- leetcode: Majority Element
- [LeetCode] Majority Element
- leetcode 153: Majority Element
- leetcode Majority Element
- LeetCode:Majority Element
- leetcode:Majority Element
- [LeetCode] Majority Element
- Leetcode-Majority Element
- [leetcode]Majority Element
- [LeetCode]Majority Element
- Leetcode: Majority Element
- 生成带链接的二维码
- Samba源码编译及安装
- 安卓自定义布局实现
- 安卓开发前后台通信,从数据库中取数据并在前台以表格形式显示,以json格式传输
- BASE64加解密
- 【leetcode】Majority Element
- 《Linux总线、设备与驱动》USBHID设备驱动
- 原版系统安装过程gpt转换mbr
- C++中map容器的说明和使用技巧
- spark 基本方法
- VS中MFC连接SQL SERVER
- 最大收益
- 使用SchemaExport生成数据表
- javascript实现网页跳转的方法