public class Solution { public IList<int> MajorityElement(int[] nums) { if(nums == null || nums.Length == 0){ return new List<int>(); } var hash = new Dictionary<int,int>(); var len = nums.Length; for (var i = 0;i < len; i++) { if(!hash.ContainsKey(nums[i])) { hash.Add(nums[i],1); } else{ hash[nums[i]]++; } } var ret = new List<int>(); foreach(var k in hash.Keys) { if(hash[k] > len / 3){ ret.Add(k); } } return ret; } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
LeetCode -- Majority Element II
原文:http://blog.csdn.net/lan_liang/article/details/49531073