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. redux和react-redux

    redux和react-redux的关系: redux是react的状态管理工具,却不仅仅只是为了react而生的,所以在使用中会存在痛点.而react-redux是专门为了react定制,目的是为了 ...

  2. test命令-linux shell 脚本

    #!/bin/bash # This is program will check you file name,which sys has or not,end print types. #2019/0 ...

  3. go语言从例子开始之Example4.常量

    Go 支持字符.字符串.布尔和数值 常量 . package main import "fmt" import "math" const 用于声明一个常量. c ...

  4. ruby之基础语法

    ruby语法之哈希 =>相当于python的字典 ruby语法之数组 =>相当于python的列表 举例: gitaly= Hash.new #建立新Hash类型 gitaly['firs ...

  5. leetcode-第14周双周赛-1273-删除树节点

    题目描述: 自己的提交:动态规划 class Solution: def deleteTreeNodes(self, nodes: int, parent: List[int], value: Lis ...

  6. Delphi 访问https /SSL、OpenSSL

    访问 Web 网站,最简单用法直接使用 TIdHTTP 控件: 例如:AA := IdHTTP1.Get('www.baidu.com.'); 访问 https 的网站,需要 SSL 库. 在 Win ...

  7. 初识localstorage用法

    最近在做一个类似填报信息的页面,一共有8页,当点击切换到下一页的时候要求把上一页的数据存到本地,以便下次切换到这个页面的时候自动把值填上去,并且在最后一页提交数据的时候直接用localstorage里 ...

  8. 【JVM】内存区域

    程序运行时,有六个地方都可以保存数据: 1. 寄存器:这是最快的保存区域,因为它位于和其他所有保存方式不同的地方:处理器内部.然而,寄存器的数量十分有限,所以寄存器是根据需要由编译器分配.我们对此没有 ...

  9. doT模板引擎

    doT模板引擎是一个比较高效的引擎,一直都在使用,只有3kb大小,简洁的语法,无任何依赖,简单易用:下面的代码直接拷贝引用就可以使用: 插件代码 (function(){function p(b,a, ...

  10. 服务安全-OAuth-OAuth2.0:百科

    ylbtech-服务安全-OAuth-OAuth2.0:百科 OAuth2.0是OAuth协议的延续版本,但不向后兼容OAuth 2.0即完全废止了OAuth1.0. OAuth 2.0关注客户端开发 ...