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.

Solution 1: 使用map计数

 class Solution {
public:
int majorityElement(vector<int>& nums) {    //runtime:32ms
map<int, int> nums_count;
vector<int>::iterator iter=nums.begin(); while(iter!=nums.end()){
++nums_count[*iter];
iter++;
} int n=nums.size(); map<int, int>::iterator ite=nums_count.begin();
//int max=ite->second;
while(ite!=nums_count.end()){
if(ite->second>n/){
return ite->first;
}
ite++;
}
}
};

Solution 2: 每找出两个不同的element就成对删除,最后可能剩两个元素或一个元素,必定都是所求

 class Solution {
public:
int majorityElement(vector<int>& nums) { //runtime: 20ms
int count=;
int ret;
for(int i=;i<nums.size();i++){
if(count==){
ret=nums[i];
count++;
}else{
if(nums[i]==ret)
count++;
else
count--;
}
}
return ret;
}
};

扩展到⌊ n/k ⌋的情况,每k个不同的element进行成对删除

【LeetCode】169 - Majority Element的更多相关文章

  1. 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore ...

  2. 【LeetCode】229. Majority Element II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...

  3. 【一天一道LeetCode】#169. Majority Element

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【LeetCode】229. Majority Element II

    Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...

  5. 【leetcode❤python】169. Majority Element

    #Method 1import math class Solution(object):    def majorityElement(self, nums):        numsDic={}   ...

  6. 【刷题-LeetCode】229. Majority Element II

    Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...

  7. 【原创】leetCodeOj --- Majority Element 解题报告(脍炙人口的找n个元素数组中最少重复n/2次的元素)

    题目地址: https://oj.leetcode.com/problems/majority-element/ 题目内容: Given an array of size n, find the ma ...

  8. LeetCode OJ 169. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  9. LeetCode Problem 169: Majority Element查找多数元素

    描述:Given an array of size n, find the majority element. The majority element is the element that app ...

随机推荐

  1. Android 禁止进入activity自动弹出键盘

    在Manifest.xml中设定activity的属性 android:windowSoftInputMode="stateHidden|stateUnchanged" 附相关属性 ...

  2. Java Map各遍历方式的性能比较

    1. 阐述 对于Java中Map的遍历方式,很多文章都推荐使用entrySet,认为其比keySet的效率高很多.理由是:entrySet方法一次拿到所有key和value的集合:而keySet拿到的 ...

  3. 使用OPENROWSET(BULK...)从平面文件加载数据

    要使用OPENROWSET首先要开启一个configure选项 sp_configure ‘show advanced options’, 1;GORECONFIGURE;GOsp_configure ...

  4. post提交/文件上传服务器修改

    第一步:修改在php5下POST文件大小的限制   1.编修php.ini   找到:max_execution_time = 30 ,这个是每个脚本运行的最长时间,单位秒,修改为: max_exec ...

  5. node-odata: 基于 NodeJS 的 REST 框架

    该开源项目目前已被 OData 官网 (odata.org)收录 关于 node-odata node-odata 可以让你轻松创建 REST API, 并能使用 OData 协议的格式进行数据的查询 ...

  6. Eclipse项目内存溢出解决方案

    方法一: 打开eclipse,选择Window--Preferences...在对话框左边的树上双击Java,再双击Installed JREs,在右边选择前面有对勾的JRE,再单击右边的“Edit” ...

  7. android下使用smack需引入的包

    compile 'org.igniterealtime.smack:smack-android:4.2.0-alpha1' compile 'org.igniterealtime.smack:smac ...

  8. 巧用ifstream判断文件是否存在

    最近在写手写数字的识别软件,训练样例数量巨大而且数字个数不唯一,有可能在中途粘出一部分做测试样例.因此写下面的脚本来获取文件名,之后丢到训练函数里. #include <algorithm> ...

  9. Linux 总结1

    ============================= 一般 ========================================= chown -R oracle:oinstall ...

  10. 51nod1394 差和问题

    我只会用线段树写...不喜欢树状数组..其实跑的也不算慢?然后各种*的时候忘了longlong一直WA...药丸! 而且我不怎么会用map离散化...那么就sort+unique #include&l ...