38- Majority Element
2016-05-01 00:01
253 查看
Majority Element My Submissions QuestionEditorial Solution
Total Accepted: 110538 Total Submissions: 268290 Difficulty: Easy
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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
思路:so easy
Total Accepted: 110538 Total Submissions: 268290 Difficulty: Easy
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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
思路:so easy
class Solution { public: int majorityElement(vector<int>& nums) { map<int,int> m; int n=nums.size(); if(n==1)return nums[0]; for(int i=0;i<nums.size();++i){ if(m.count(nums[i])){ m[nums[i]]++; if(m[nums[i]]>nums.size()/2)return nums[i]; } else m[nums[i]]=1; } } };
相关文章推荐
- 2016 UESTC Training for Data Structures C - 卿学姐与诡异村庄 并查集
- 2016 UESTC Training for Data Structures B - 卿学姐与基本法 自己构建了一个和堆有点像的数据结构
- 2016 UESTC Training for Data Structures A - 卿学姐与公主 线段树
- 使用Git进行团队开发
- 大道至简阅读笔记08-完结
- ubuntu 下扫描同一局域网的IP
- 接地
- 网络编程学习——数据链路访问
- 网络编程学习——客户/服务器程序设计范式(一)
- 网络编程学习——一些辅助函数
- Go 性能优化技巧 5/10
- Go 性能优化技巧 7/10
- JAVA_TCP_HTTP_Get_Data_Baidu
- UWP 部署失败,因为未检测到任何Windows Phone (ARM->Device)
- ELK安装使用
- hibernate 关于联合主键
- Bestcoder #82 Div2 ztr loves lucky numbers(next_permutation)
- Commission
- Selenium 测百度
- Centos7 修改运行级别