169. Majority Element
Element 169
2023-09-11 14:22:45 时间
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
Approach #1: HashMap. [C++]
class Solution { public: int majorityElement(vector<int>& nums) { map<int, int> mp; int n = nums.size(); int ans = 0; for (int num: nums) mp[num]++; map<int, int>::iterator it; it = mp.begin(); while (it != mp.end()) { if (it->second > n/2) ans = it->first; it++; } return ans; } };
相关文章
- Leetcode: Find Peak Element
- Make jQuery throw error when it doesn't match an element
- vue管理系统(element-admin)在render函数中使用指令
- element-ui表格单元格合并后数据出现偏移问题及导出后合计行错位
- vue+element Form键盘回车事件页面刷新解决
- element-ui tree实现选中、高亮、定位的功能记录贴
- 【LeetCode】229. Majority Element II
- css 修改element组件自动生成后的样式
- [WinForm] 使用 WebBrowser 操作 HTML 頁面的 Element-摘自网络
- [LeetCode] 1157. Online Majority Element In Subarray 子数组中占绝大多数的元素
- [LeetCode] 34. Find First and Last Position of Element in Sorted Array 在有序数组中查找元素的第一个和最后一个位置
- element 上传文件 文件列表只显示最新的一条
- vue+element 构建的后台管理系统项目(1)新建项目
- 162. Find Peak Element
- leetcode 503. Next Greater Element II 下一个更大元素 II(中等)