Description

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: [,,]
Output:

Example 2:

Input: [,,,,,,]
Output:

问题描述: 给定一个非空数组,找到出现次数最多的元素 超过N/2次

思路: 用最简单粗暴的方法,遍历数组,将每个元素出现的次数和元素作为键值对的形式保存起来,最后再遍历键值对,找出出现次数最多的元素。

代码:

public static int MajorityElement(int[] nums)
{
Dictionary<int, int> dics = new Dictionary<int, int>();
int result = int.MinValue;
int maxCount = ;
for (int i = ; i < nums.Length; i++)
{
if(dics.ContainsKey(nums[i]))
{
dics[nums[i]] = dics[nums[i]] + ;
}
else
{
dics.Add(nums[i], );
}
}
foreach (KeyValuePair<int,int> item in dics)
{
if (item.Value > maxCount)
{
result = item.Key;
maxCount = item.Value;
} }
return result;
}

但是这种解法用到的键值对,思考是否有更好的解法,看了别人的解法。更为简单和简洁。思路就是假设第一个元素为多数元素,遍历数组,如果下一个元素和多数元素相同,则数量加一,如果不想等,数量减一,如果count==0 则修改多数元素为当前元素。

下面是代码。

public int MajorityElement(int[] nums) {
int majority = nums[], count = ;
for (int i=; i<nums.Length; i++)
{
if (count == )
{
count++;
majority = nums[i];
}
else if (majority == nums[i])
{
count++;
}
else
count--;
} return majority;
}

这种解法是在给定数组中元素只有两个值。 如果出现三个值就不可以了

但是题目并未给出这个条件,只是在实例中体现出来了。

LeetCode Array Easy169. Majority Element的更多相关文章

  1. 【LeetCode 229】Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

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

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

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

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

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

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

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

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

  6. (Array)169. Majority Element

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

  7. Leetcode # 169, 229 Majority Element I and II

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

  8. LeetCode【169. Majority Element】

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

  9. 【LeetCode】169 - Majority Element

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

随机推荐

  1. poj3264 Balanced Lineup(树状数组)

    题目传送门 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 64655   Accepted: ...

  2. db2序列

    CREATE SEQUENCE <sequence-name> AS data-type   默认 As Integer START WITH <numeric-constant&g ...

  3. c# 编程--基础部分补全篇

    C#基础 分支: switch            switch(表达式)                {                    case 具体值1:                ...

  4. 【记录】解决uni-app 用nginx反向代理出现Invalid Host header问题

    之前解决过一次,后来给忘记了,今天又遇到这个问题,现记录一下 修改uni-app的manifest.json文件  - >源码视图 添加以下代码: "disableHostCheck& ...

  5. 在eclipse中添加svn插件

    1.点击菜单栏中的help选项,然后选择Install New Software,然后点击ADD,输入: name:subclipse     url:http://subclipse.tigris. ...

  6. ORACLE 11G新特性之一(增加带default的字段)

    在11g之前,增加带default值的字段,实现原理如下: alter table t1 add c1 varchar2(20) default 'XX' not null; 假设t1表有4千万行数据 ...

  7. php结合Redis实现100万用户投票项目,并实时查看到投票情况的案例

    场景:某网站需要对其项目做一个投票系统,投票项目上线后一小时之内预计有100万用户进行投票,希望用户投票完就能看到实时的投票情况 这个场景可以使用redis+mysql冷热数据交换来解决. 何为冷热数 ...

  8. 配置框架spring和SpringDataJpa整合----员工是爹

    <!-- 1.dataSource 配置数据库连接池--> <bean id="dataSource" class="com.mchange.v2.c3 ...

  9. tomcat启动、停止和重启脚本

    脚本名称:r.sh 脚本用途:启动.停止和重启tomcat 脚本参数:$1:[start|stop|restart] #!/bin/bash BIN_PATH="/tomcat_path/b ...

  10. shell--grep命令+正则表达式+基本语法

    什么是正则 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则. 在linux中,通配符是由shell解释的,而正则表达式则 ...