public int MajorityElement(int[] nums) { Dictionary<int, int> dic = new Dictionary<int ,int>(); foreach (int num in nums) { if(!dic.ContainsKey(num)) { dic.Add(num, 0); } dic[num]++; } int m = 0; int n = nums.Length; foreach(KeyValuePair<int,int> k in dic) { if(k.Value>n/2) { m = k.Key; } } return m; }
public int MajorityElement(int[] nums) { Array.Sort(nums); return nums[nums.Length/2]; }
标签:return,Dictionary,nums,int,元素,dic,num,多数 From: https://www.cnblogs.com/Dongmy/p/18181129