算法第二周Majority Element[easy]
来源:互联网 发布:淘宝茶叶店标图片 编辑:程序博客网 时间:2024/06/07 17:34
Majority Element[Easy]
Description
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.
Analysis
这个问题比较简单,主要是计算数组中数字出现的频率,我使用map来记录数字在数组中出现的次数,要注意本题要求次数大于 ⌊ n/2 ⌋ 次。
Solution
class Solution {public: int majorityElement(vector<int>& nums) { map<int, int> record; int l = nums.size()/2; int val = 0; for (auto iter = nums.begin(); iter != nums.end(); iter++) { record[*iter]++; if (record[*iter] > l ) { val = *iter; break; } } return val; }};
阅读全文
0 0
- 算法第二周Majority Element[easy]
- 算法课第二周作业 | Majority Element
- (算法分析Week1)Majority Element[Easy]
- Majority Element (easy)
- 169. Majority Element (Easy)
- 169. Majority Element Easy
- 第二周:[LeetCode]169. Majority Element
- [Leetcode 169, Easy] Majority Element
- <LeetCode><Easy> 169 Majority Element
- 169. Majority Element [easy] (Python)
- 【LeetCode】169.Majority Element (Easy)
- Leetcode 169. Majority Element[easy]
- Easy 169题 Majority Element
- LeetCode 169. Majority Element (Easy)
- LeetCode 169. Majority Element (Easy)
- 【Leetcode-Easy-169】 Majority Element
- 算法第六周Majority Element II[medium]
- [ 算法 ] Majority Element
- Java中的构造代码块、静态代码块、普通代码块的区别
- css基础入门
- JavaMail使用心得
- mac频繁掉线的解决方案汇总
- jquery实现全选、全不选、反选
- 算法第二周Majority Element[easy]
- pullToRefresh
- MySQL InnoDb中的 Clustered Index 和 Secondary Index
- 移动商城第九篇【属性选项卡、最小存储单元、数据进库】
- 作用域闭包
- 输入任意一正整数,输入该数的质数相乘的形式
- Tensorflow-Graph
- Calico 的默认连通性
- 推送(消息推送)