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. UE中使用正则表达式的一些技巧

    UE中使用正则表达式的一些技巧 2010-12-24 10:33:19 分类: Linux 以下是网上摘录的UE 技巧 1)删除空行: 替换 %[ ^t]++^p 为 空串  2)删除行尾空格: 替换 ...

  2. SQL SERVER ->> CXPacket等待类型

    最近做了一个项目,把整个数据仓库平台下所有的表和索引都改成页级别的数据压缩.昨天发现测试环境下的某个workload跑得比平时慢.最后我们定位了到这个workload做的事情中可能造成性能下降的地方, ...

  3. redis 2.4异常

    最近公司redis服务出现了异常,记录下教训: redis异常后:观察redis服务,可以看到redis cpu占用100% 用strace命令查看redis进程,显示如下: open("/ ...

  4. android ImageView的属性android:scaleType,即ImageView.setScaleType(ImageView.ScaleType)

    实例 <ImageView android:id="@+id/image" android:layout_width="fill_parent" andr ...

  5. 路由器扫描的Java源码

    这个源码不是本人写的,是我原来的领导写的,我们都叫他东哥,这个是东留给我的一个小资源,好佩服他哦,这个东西可以用来扫描全世界的路由器,破解路由器账户和密码 当然是简单的了.我能力不够没有更完善的补充下 ...

  6. Just Have a Change

    If you still do something meaningless or live a purposeless and empty life. Now, it may be time for  ...

  7. Hibernate 异常 —— No CurrentSessionContext configured

    在使用 SessionFactory 的 getCurrentSession 方法时遇到如下异常 “No CurrentSessionContext configured ” 原因是: 在hibern ...

  8. python 列表(list)去除重复的元素总结

    方法一: 将list作为set的构造函数构造一个set,然后再将set转换会list就可以 >>> myList = [1, 2, 3, 3, 2, 2, 4, 5, 5] > ...

  9. wget 批量下载目录文件

    wget -r -p -k -np http://源目录     ./本地目标目录

  10. mysql连接超时

    这几天在跟踪一个项目的时候,老是发现mysql连接报timeout的异常. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException ...