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. Linux安装配置nfs实现共享远程目录

    1. 服务端安装nfs yum -y install nfs-utils rpcbind 2.编辑/etc/exports /etc/exports文件内容格式: <输出目录> [客户端1 ...

  2. Nuget--基础连接已经关闭

    1.Nuget---基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系 修改一下 Package Source 改为 http://packages.nuget.org 2.Nuget- ...

  3. php yield关键字以及协程的实现

    php的yield是在php5.5版本就出来了,而在初级php界却很少有人提起,我就说说个人对php yield的理解 Iterator接口 在php中,除了数组,对象可以被foreach遍历之外,还 ...

  4. dotNET面试(二)

    值类型与引用类型 1.值类型和引用类型的区别? 值类型包括简单类型.结构体类型和枚举类型,引用类型包括自定义类.数组.接口.委托等. 赋值方式:将一个值类型变量赋给另一个值类型变量时,将复制包含的值. ...

  5. Sass--嵌套选择器

    Sass 中还提供了选择器嵌套功能,但这也并不意味着你在 Sass 中的嵌套是无节制的,因为你嵌套的层级越深,编译出来的 CSS 代码的选择器层级将越深,这往往是大家不愿意看到的一点.这个特性现在正被 ...

  6. excel导出简单示例(jxl jar包)

    @param title excel文件名 @param excelTopName 表头中文名字(显示在第一行的中文表头内容) @param header 表头字段属性(根据该属性获取对应的属性值,表 ...

  7. 关于LCN分布式事务框架

    基于LCN框架解决分布式事务 LCN官网 https://www.txlcn.org/ "LCN并不生产事务,LCN只是本地事务的搬运工" 兼容 dubbo.springcloud ...

  8. vs code常用插件(python)

    1.chinese 作用:vscode设置为中文. 使用方法:Ctrl+Shift+P:输入 "config":选择zh 2.python 作用:调试 3.autoDocstrin ...

  9. 最佳实践 | RDS & POLARDB归档到X-Pack Spark计算

    X-Pack Spark服务通过外部计算资源的方式,为Redis.Cassandra.MongoDB.HBase.RDS存储服务提供复杂分析.流式处理及入库.机器学习的能力,从而更好的解决用户数据处理 ...

  10. Struts2 简单的增删改查

    1:主页面 <a href="emp-list">emp-list</a> <br> 然后到struts.xml文件中找到对应的emp-list ...