C#LeetCode刷题之#169-求众数(Majority Element)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4048 访问。
给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ 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)的更多相关文章
- [Swift]LeetCode169. 求众数 | Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Leetcode之分治法专题-169. 求众数(Majority Element)
Leetcode之分治法专题-169. 求众数(Majority Element) 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是 ...
- 【Leetcode】【简单】【169求众数】【JavaScript】
题目 169. 求众数 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [ ...
- LeetCode刷题总结-数组篇(下)
本期讲O(n)类型问题,共14题.3道简单题,9道中等题,2道困难题.数组篇共归纳总结了50题,本篇是数组篇的最后一篇.其他三个篇章可参考: LeetCode刷题总结-数组篇(上),子数组问题(共17 ...
- C#LeetCode刷题-位运算
位运算篇 # 题名 刷题 通过率 难度 78 子集 67.2% 中等 136 只出现一次的数字 C#LeetCode刷题之#136-只出现一次的数字(Single Number) 53.5% 简单 ...
- C#LeetCode刷题-分治算法
分治算法篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- C#LeetCode刷题-树
树篇 # 题名 刷题 通过率 难度 94 二叉树的中序遍历 61.6% 中等 95 不同的二叉搜索树 II 43.4% 中等 96 不同的二叉搜索树 51.6% 中等 98 验证二叉搜索树 ...
- leetcode刷题目录
leetcode刷题目录 1. 两数之和 2. 两数相加 3. 无重复字符的最长子串 4. 寻找两个有序数组的中位数 5. 最长回文子串 6. Z 字形变换 7. 整数反转 8. 字符串转换整数 (a ...
随机推荐
- Cyber Security - Palo Alto Firewall Objects Addresses, Services, and Groups(1)
Address Objects and Groups Creating address objects. Organizing address objects with address groups ...
- Ethical Hacking - NETWORK PENETRATION TESTING(7)
Gaining Access to encrypted networks Three main encryption types: 1. WEP 2.WPA 3.WPA2 WEP Cracking W ...
- 12C oracle 12.1.0.2版本打补丁
从support 1454618.1文档 中可以下载到各版本的补丁和PSU. 此次采用的是28349311 版本号的psu 和28440711 版本号的jvm.opatch版本号6880880_12 ...
- 题解 CF1372C
题目 传送门 题意 给你一个 \(1\) 到 \(n\) 的排列. 定义特殊交换为:选择一段区间\([l,r]\) ,使得此段区间上的数交换后都不在原来的位置. 问最少多少次可以将此排列变成升序的. ...
- .clearfix 清除浮动,@import
我们知道,在网页的DIV+CSS布局中,很多时候要用到浮动. 既然有浮动,那就有清除浮动. 清除浮动有很多种方式,而在实际项目中,比较常用的是这一种. .clearfix:after { conten ...
- C++语法小记---类模板
类模板 类模板和函数模板类似,主要用于定义容器类 类模板可以偏特化,也可以全特化,使用的优先级和函数模板相同 类模板不能隐式推倒,只能显式调用 工程建议: 模板的声明和实现都在头文件中 成员函数的实现 ...
- 题解 洛谷 P2046 【[NOI2010]海拔】
首先进行贪心,发现海拔有梯度时一定是不优的,最优的情况是海拔像断崖一样上升,也就是左上角有一片海拔高度为\(0\),右下角有一片海拔高度为\(1\). 发现这样的性质后,不难想到用最小割来解决问题,但 ...
- python dict乱码如何解决
定义字典并直接输出,结果输出结果中文是乱码展示 d={'name':'lily','age':18,'sex':'女','no':1121} print d 输出结果: {'age': 18, 'no ...
- windy数(数位dp)
https://www.luogu.com.cn/blog/virus2017/shuweidp https://www.luogu.com.cn/problem/P2657 #include < ...
- 大一寒假我在一个oj网站只刷了这些题从此入门了绝大部分通用算法
如果你想入门算法,那么我这篇文章也许可以帮到你. 先说点题外话.这是在一个不冷不热的寒假,照理来说寒假应该很冷,但这个寒假是真的舒服.这样舒服的寒假学习似乎是一件不可能的事情,所以我继续我的游戏生涯, ...