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. SQL Server 高性能写入的一些总结

    1.1.1 摘要 在开发过程中,我们不时会遇到系统性能瓶颈问题,而引起这一问题原因可以很多,有可能是代码不够高效.有可能是硬件或网络问题,也有可能是数据库设计的问题. 本篇博文将针对一些常用的数据库性 ...

  2. ACMer(转)

    我所了解的ACMer主要分为以下几类: A类:天才型(这个在咱们学校基本不存在),所以就不用考虑了-_- ! B类:刷题很快,题数是最多的几个之一,但有一个习惯就是喜欢看题解,思考问题一想不出来了就想 ...

  3. Allegro16.3约束设置

    差分对的约束设置 第一步,差分对的设置 差分对的设置有很多方法,下面介绍两种最常用的方法. 1. 点击菜单Logic→Assign Differential Pair... 弹出以下对话框. 点击你想 ...

  4. js收藏

    //设为主页function SetHome(obj, vrl) { try { obj.style.behavior = 'url(#default#homepage)'; obj.setHomeP ...

  5. 在服务器端保存ViewState

    1.比较 Asp.net 的一个强大的功能就是ViewState,  但是这也成为很多人诟病的地方,我们先来看一个实例 ViewState其中一个特性就是保存页面的状态,下面我们看一个很简单的登录页面 ...

  6. pl/sql programming 03 语言基础

    PL/SQL 块结构 最小的有意义的代码单元叫做 块(block). 一个块是一组代码, 这个块给出了执行边界, 也为变量声明和异常处理提供了作用范围, pl/sql 准许我们创建匿名块和命名块, 命 ...

  7. D:/apache2/conf/httpd.conf:Cannot load D:/apache2/modules/mod_actions.so

    报错如下: errors reported here must be corrected before service can be started.httpd:Syntax error on lin ...

  8. HibernateTemplate 查询

    Spring中常用的hql查询方法getHibernateTemplate()上     一.find(String queryString);   示例:this.getHibernateTempl ...

  9. Windows 下搭建LDAP服务器

    五一闲来没事,加上项目正在进行UAT.抽空研究了一下LDAP相关知识.随手做一个记录. 为了方便阅读还是先介绍一下什么是LDAP? 前言.Lightweight Directory Access Pr ...

  10. HDU 1074 Doing Homework

    第一次做这道题大概是半个月前了吧,状压DP一个很新鲜的名词 当时看题解怎么也看不懂,现在看懂了以后还是很简单的 所谓状态压缩就是用一个整数的二进制来表示一个状态,比如有三个作业 000表示一科作业也没 ...