對比python和java的字典數據結構,如下就LeetCode面試題 17.10. 主要元素爲栗子,進行學習。是一道簡單題目,重點看數據結構的運用與實現。java
普通的思路,使用一個字典結構記錄每一個元素出現的次數,而後判別每一個元素出現次數是否超過數組長度的一半(絕對大於)。python
python實現dict:咱們能夠看到,很是簡單靈活,而又清晰面試
1 class Solution: 2 def majorityElement(self, nums: List[int]) -> int: 3 if not nums: return -1 4 hashtable = {} 5 n = len(nums) 6 for i in range(n): 7 if nums[i] not in hashtable.keys(): 8 hashtable[nums[i]] = 1 9 else: 10 hashtable[nums[i]] += 1 11 if hashtable[nums[i]] > n//2: 12 return nums[i] 13 return -1
java使用HashMap實現dict邏輯:重點要注意HashMapde的定義,Integer不能寫成int(親測會報錯)。判斷元素是否存在使用HashMap.containsKey()函數;獲得相應元素使用get函數,改變指定元素使用put函數。算法
1 class Solution { 2 public int majorityElement(int[] nums) { 3 HashMap<Integer, Integer> hashtable = new HashMap<>(); 4 int n = nums.length; 5 for(int i = 0; i < n; i++){ 6 if (hashtable.containsKey(nums[i])){ 7 int tmp = hashtable.get(nums[i]); 8 tmp++; 9 hashtable.put(nums[i], tmp); 10 }else{ 11 hashtable.put(nums[i], 1); 12 } 13 if(hashtable.get(nums[i]) > n/2){ 14 return nums[i]; 15 } 16 } 17 return -1; 18 } 19 }
第三行代碼可使用Map(父類)進行定義:數組
Map<Integer, Integer> hashtable = new HashMap<>();
發現了java更簡潔的寫法,使用了HashMap中的getOrDefault方法。數據結構
1 class Solution { 2 public int majorityElement(int[] nums) { 3 Map<Integer,Integer> map=new HashMap<>(); 4 for(int i=0;i<nums.length;i++) { 5 map.put(nums[i], map.getOrDefault(nums[i],0)+1); 6 if(map.get(nums[i])>nums.length/2)return nums[i]; 7 } 8 return -1; 9 10 } 11 } 12 13 做者:camile8 14 連接:https://leetcode-cn.com/problems/find-majority-element-lcci/solution/zhi-xing-yong-shi-20-msnei-cun-xiao-hao-442-mb-by-/ 15 來源:力扣(LeetCode) 16 著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。
小小臺階:題目有個小進階,以下圖函數
首先作個井底之蛙,不看大佬的算法講解,先排序,而後使用pre和count進行元素數量的記錄,最後判斷,由於排序最小的時間複雜度是nlog(n),所以須要進行算法優化啦,:學習
1 class Solution: 2 def majorityElement(self, nums: List[int]) -> int: 3 nums.sort() 4 if not nums: return -1 5 pre = nums[0] 6 index = 1 7 count = 1 8 while index < len(nums): 9 if nums[index] == pre: 10 count += 1 11 else: 12 pre = nums[index] 13 count = 1 14 index += 1 15 if count > len(nums)//2: 16 return pre 17 if count > len(nums)//2: 18 return pre 19 return -1
java實現:java實現涉及幾個知識點,首先,java的排序,而後呢,java的for循環數組遍歷。這裏呢,咱們對list進行排序,使用Arrays類的sort方法。優化
1 class Solution { 2 public int majorityElement(int[] nums) { 3 Arrays.sort(nums); 4 if(nums == null){return -1;} 5 int pre = nums[0], count = 1; 6 for(int i = 1; i < nums.length; i++){ 7 if(nums[i] == pre){ 8 count++; 9 }else{ 10 pre = nums[i]; 11 count = 1; 12 } 13 if (count > nums.length/2){ 14 return nums[i]; 15 } 16 } 17 if (count > nums.length/2){ 18 return pre; 19 } 20 return -1; 21 } 22 }
算法優化:利用找衆數的思想,「世界男人分兩種,我和我之外的」,特殊狀況下[1, 2, 2, 3, 3, 3],須要進行驗證,存在的衆數數量是否大於總量一半。spa
python代碼:
1 class Solution: 2 def majorityElement(self, nums: List[int]) -> int: 3 n = len(nums) 4 if n==0: return -1 5 tmp, count = nums[0], 1 6 for i in range(1, n): 7 if nums[i] == tmp: 8 count += 1 9 else: 10 count -= 1 11 if count == 0: 12 tmp = nums[i] 13 count = 1 14 if count==0: return -1 15 half_num = n //2 + 1 16 count = 0 17 for i in range(n): 18 if nums[i] == tmp: 19 count += 1 20 if count == half_num: 21 return tmp 22 return -1
java代碼:
1 class Solution { 2 public int majorityElement(int[] nums) { 3 int n = nums.length; 4 if(n==0){return -1;} 5 int tmp = nums[0], count = 1; 6 for(int i = 1; i < n; i++){ 7 if(nums[i]==tmp){ 8 count++; 9 } 10 else{ 11 count--; 12 } 13 if(count==0){ 14 tmp = nums[i]; 15 count = 1; 16 } 17 } 18 if(count == 0){ return -1;} 19 int half_n = n/2 + 1; 20 count = 0; 21 for(int i = 0; i < n; i++){ 22 if(nums[i] == tmp){count++;} 23 if(count==half_n){ 24 return tmp; 25 } 26 } 27 return -1; 28 } 29 }