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. Python 读书

    第一章 %d %s %f 数字和表达式 加减乘取模都可以直接输入 除需注意: 1/2=0.5 1/2.0=0.5 --有浮点按浮点计算 1//2=0 --整除 1.0/2.0=0.5 1.0//2.0 ...

  2. border-radius使用的一些问题(不起作用?)

    出现这种问题,border-radius没有达到自己想要的效果(小程序中) 原因:border-radius定义了border的圆角,未定义span元素的圆角范围 解决:不设置border,则bord ...

  3. getopts的执行过程

  4. 2019牛客暑期多校训练营(第九场)A.The power of Fibonacci

    题意:给出n和m,f(n)是斐波那契额数列的第n项,要求计算ans=f(1)^m+f(2)^m+....f(n)^m.即斐波那契额数列前n项的m次幂和. 解法:这题好像有两种解法:一种是循环节+CRT ...

  5. 不修改源代码,动态注入Java代码的方法(转)

    转自:https://blog.csdn.net/hiphoon_sun/article/details/38707927 有时,我们需要在不修改源代码的前提下往一个第三方的JAVA程序里注入自己的代 ...

  6. Hibernate中Session的save()、update()、merge()、lock()、saveOrUpdate()和persist()方法有什么区别?

    Hibernate的对象有三种状态:瞬态.持久态和游离态.游离状态的实例可以通过调用save().persist()或者saveOrUpdate()方法进行持久化:脱管状态的实例可以通过调用 upda ...

  7. Java缓冲流的优点和原理

    不带缓冲的流的工作原理: 它读取到一个字节/字符,就向用户指定的路径写出去,读一个写一个,所以就慢了. 带缓冲的流的工作原理: 读取到一个字节/字符,先不输出,等凑足了缓冲的最大容量后一次性写出去,从 ...

  8. Web源码泄露总结

    Web源码泄露总结 背景 本文主要是记录一下常见的源码泄漏问题,这些经常在web渗透测试以及CTF中出现. 源码泄漏分类 .hg源码泄漏 漏洞成因: hg init的时候会生成.hg e.g.http ...

  9. 【leetcode】435. Non-overlapping Intervals

    题目如下: Given a collection of intervals, find the minimum number of intervals you need to remove to ma ...

  10. 版本控制系统之SVN和GIT的区别

    版本控制器的作用: 1. 可以协同代码管理,让多人开发代码得以实现. 2. 回归到以前的任何一个时间点的代码处(好比:开始写了很多代码,后面有修改了一些,突然IDE崩溃,但是发现还是以前的代码更好,这 ...