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. 棋盘问题(DFS)& Dungeon Master (BFS)

    1棋盘问题 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的 ...

  2. 图片查看器(类似于QQ,另外又加了JARA的下方的图片缩略导航图)

    源码地址:https://gitee.com/yolanda624/coffer/tree/master/src/components/a-photo-view

  3. js实现图片延迟加载原理

    <img src="image/1188695.png" alt="taobao" trueImg="image/1.jpg" id= ...

  4. ArrayListMultimap

    遇到这样一个场景,就是要判断传过来的Lists里有几组相邻的元素,然后有多少个单一的元素,比如3,3,3,4,4,4,5,5,5,6,6,6,8,9,10,11 方法有很多,但是我选了ArrayLis ...

  5. iview table绑定双击事件

    <Table <Table ref="table" highlight-row :columns="columns" :data="new ...

  6. hadoop 2.7 添加或删除datanode节点

    1.测试环境 ip 主机名 角色 10.124.147.22 hadoop1 namenode 10.124.147.23 hadoop2 namenode 10.124.147.32 hadoop3 ...

  7. array_shift — 将数组开头的单元移出数组

    <?php $stack = array("orange", "banana", "apple", "raspberry&q ...

  8. 【杂】聊聊我的男神:Jordan Peterson

    这篇文章我打算聊聊我的男神Jordan Peterson(简称JP).如果还不太了解JP,那么下面两个链接是JP的背景介绍: [文字]Jordan Peterson From Wikipedia, t ...

  9. Delphi 格式化函数 Format函数

    function Format(const Format: string; const Args: array of const): string; function Format(const For ...

  10. SQL Wildcards 通配符

    SQL Wildcards通配符 通配符用于替换字符串中的任何其他字符. 通配符与SQL LIKE运算符一起使用.在WHERE子句中使用LIKE运算符来搜索列中的指定模式. 有两个通配符与LIKE运算 ...