问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4048 访问。

给定一个大小为 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。

你可以假设数组是非空的,并且给定的数组总是存在众数。

输入: [3,2,3]

输出: 3

输入: [2,2,1,1,1,2,2]

输出: 2


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.

Input: [3,2,3]

Output: 3

Input: [2,2,1,1,1,2,2]

Output: 2


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4048 访问。

public class Program {

    public static void Main(string[] args) {
int[] nums = { 2, 2, 1, 1, 1, 2, 2 }; var res = MajorityElement(nums);
Console.WriteLine($"{res}"); res = MajorityElement2(nums);
Console.WriteLine($"{res}"); res = MajorityElement3(nums);
Console.WriteLine($"{res}"); Console.ReadKey();
} private static int MajorityElement(int[] nums) {
//哈希法
var dic = new Dictionary<int, int>();
int maxCount = nums.Length / 2;
for(int i = 0; i < nums.Length; i++) {
if(dic.ContainsKey(nums[i])) {
dic[nums[i]]++;
} else {
dic[nums[i]] = 1;
}
if(dic[nums[i]] > maxCount) {
return nums[i];
}
}
return -1;
} private static int MajorityElement2(int[] nums) {
int result = 0;
for(int i = 0, count = 0; i < nums.Length; i++) {
if(count == 0 || result == nums[i]) {
//当计数为0时,重置计数和结果
//当上一次记录的值和本次相同时,增加计数
count++;
result = nums[i];
} else {
//当上一次记录的值和本次不相同时,减少计数
count--;
}
}
//最后记录的值,一定是众数
return result;
} private static int MajorityElement3(int[] nums) {
//基于一个事实,排序后的数组最中间的那个数一定是众数
Array.Sort(nums);
return nums[nums.Length / 2];
} }

以上给出3种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4048 访问。

2
2
2

分析:

显而易见,MajorityElement和MajorityElement2的时间复杂度均为:  ,MajorityElement3的时间复杂度基于Array.Sort()所使用的排序算法。

C#LeetCode刷题之#169-求众数(Majority Element)的更多相关文章

  1. [Swift]LeetCode169. 求众数 | Majority Element

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

  2. Leetcode之分治法专题-169. 求众数(Majority Element)

    Leetcode之分治法专题-169. 求众数(Majority Element) 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是 ...

  3. 【Leetcode】【简单】【169求众数】【JavaScript】

    题目 169. 求众数 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [ ...

  4. LeetCode刷题总结-数组篇(下)

    本期讲O(n)类型问题,共14题.3道简单题,9道中等题,2道困难题.数组篇共归纳总结了50题,本篇是数组篇的最后一篇.其他三个篇章可参考: LeetCode刷题总结-数组篇(上),子数组问题(共17 ...

  5. C#LeetCode刷题-位运算

    位运算篇 # 题名 刷题 通过率 难度 78 子集   67.2% 中等 136 只出现一次的数字 C#LeetCode刷题之#136-只出现一次的数字(Single Number) 53.5% 简单 ...

  6. C#LeetCode刷题-分治算法

    分治算法篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...

  7. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

  8. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  9. C#LeetCode刷题-树

    树篇 # 题名 刷题 通过率 难度 94 二叉树的中序遍历   61.6% 中等 95 不同的二叉搜索树 II   43.4% 中等 96 不同的二叉搜索树   51.6% 中等 98 验证二叉搜索树 ...

  10. leetcode刷题目录

    leetcode刷题目录 1. 两数之和 2. 两数相加 3. 无重复字符的最长子串 4. 寻找两个有序数组的中位数 5. 最长回文子串 6. Z 字形变换 7. 整数反转 8. 字符串转换整数 (a ...

随机推荐

  1. spring oauth2获取当前登录用户信息。

    使用spring oauth2框架做授权鉴定.想获取当前用户信息怎么办? 我们知道spring oauth2是基于spring security的实现的. spring security可以通过Sec ...

  2. 【Python学习笔记三】一个简单的python爬虫

    这里写爬虫用的requests插件 1.一般那3.x版本的python安装后都带有相应的安装文件,目录在python安装目录的Scripts中,如下:   2.将scripts的目录配置到环境变量pa ...

  3. Cooperative Groups

    Cooperative Groups 目录 Cooperative Groups 简介 块内组 thread_block tiled_partition Thread Block Tiles Coal ...

  4. pom.xml文件中的parent标签

    基本概念 maven的核心就算pom.xm,使用maven是为了更好地帮项目管理包依赖.如果要引入一个jar包,需要在pom文件中加上 <dependency> <groupId&g ...

  5. 2. 妈呀,Jackson原来是这样写JSON的

    没有人永远18岁,但永远有人18岁.本文已被 https://www.yourbatman.cn 收录,里面一并有Spring技术栈.MyBatis.JVM.中间件等小而美的专栏供以免费学习.关注公众 ...

  6. 关于页面布局中,如何让一个div水平和垂直居中的五个方案

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 3.pandas的简单查询

    知道了基本的pandas的数据结构,就可以进行查询相应的数据了 DataFrame可以看成是一个个的Series组成的一个二维结构,既然如此,就会有从DataFrame里查询Series的方法 从Da ...

  8. 轻松应对并发问题,Newbe.Claptrap 框架中 State 和 Event 应该如何理解?

    Newbe.Claptrap 框架中 State 和 Event 应该如何理解?最近整理了一下项目的术语表.今天就谈谈什么是 Event 和 State. Newbe.Claptrap 是一个用于轻松 ...

  9. lua中 table.getn(t) 、#t、 table.maxn(t) 这三个什么区别?

    lua中 table.getn(t) .#t. table.maxn(t) 这三个什么区别? RTlocal t = {1,888,x= 999,b=2,5,nil,6,7,[10]=1,8,{z = ...

  10. C#怎么统计网站当前在线人数

    1.问题背景 c#网站怎么合理的统计在线人数?我想通过全局变量来统计软件的使用情况,当启动软件时向服务器的用户表写开始使用时间,正常退出时写一个结束使用时间,来统计用户的在线使用情况. 但是有一个问题 ...