-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathMajorityNumberSln.cs
55 lines (51 loc) · 1.57 KB
/
MajorityNumberSln.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DP.Lib
{
// 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.
public class MajorityNumberSln
{
//O(1)空间复杂度
public int MajorityElement(int[] nums)
{
int major = nums[0];
int count = 1;
for (int i = 1; i < nums.Length; i++)
{
if (count == 0)
{
count++;
major = nums[i];
}
else if (major == nums[i])
count++;
else
count--; //此消彼长
}
return major;
}
//hash table version
public int MajorityElement2(int[] nums)
{
Dictionary<int, int> dict = new Dictionary<int, int>();
int majorityElement = nums[0];
foreach (var item in nums)
{
if (dict.ContainsKey(item))
{
dict[item]++;
//注意是大于,而不是 大于或等于
if (dict[item] > nums.Length / 2)
majorityElement = item;
}
else
dict.Add(item, 1);
}
return majorityElement;
}
}
}