[169] Majority Element

1. 題目描述

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.

給定一個數組,找到數組中的主要元素,即這個元素佔數組的總元素個數的1/2以上。

2. 解題思路

方案1:很直接的想法,遍歷數組,邊遍歷邊統計數組中每個元素的個數,最後找到元素個數最多的那個。本文使用的方法就是使用一個HashMap進行統計,因爲HashMap本身並不能根據value進行排序。所以我們將一個map轉換爲一個list,使用java自帶的Collections.sort函數進行排序,之後將最大值輸出。
方案2:優秀的解決方案。想法是,給定一個緩存,當前緩存中沒有元素時,將讀入的值加入緩存並計數爲1,繼續讀入,噹噹前值等於當前緩存中那個值時,當前緩存的值得計數加1,反之減1,當計數值爲0時,將緩存清空,按照上述過程做下來最後剩下的那個值即爲數組中最多的那個元素。如,給定[1,2,1,3,1,1,2]那麼過稱如下:

讀入數字 緩存中的數字 計數
1 1 1
2 1 0
1 1 1
3 1 0
1 1 1
1 1 2
2 1 1

最後緩存中的數字爲1,則最多的元素爲1。

3. Code

// Code1:方案1
import java.util.HashMap;
import java.util.Collections;

public class Solution {
    public int majorityElement(int[] nums) {
        HashMap<Integer, Integer> hashMap = new HashMap<>();
        // 遍歷所有值並統計數量加入到map中
        for(int i = 0; i < nums.length; ++i)
        {
           if(!hashMap.containsKey(nums[i]))
           {
               hashMap.put(nums[i], 1);
           }else
           {
               hashMap.put(nums[i], hashMap.get(nums[i]) + 1);
           }
        }
        // 將map轉換爲一個list
        List<Map.Entry<Integer, Integer>> list = new ArrayList<>(hashMap.entrySet());
        // 使用Collections內置的sort函數對List進行排序
        Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>()
        {
           public int compare(Map.Entry<Integer, Integer> value1, Map.Entry<Integer, Integer> value2)
           {
               // 根據value值從大到小排序
               return value2.getValue() - value1.getValue();
           }
        });
        return list.get(0).getKey();
    }
}
// Code2:方案2
public class Solution {
    public int majorityElement(int[] nums) {
        // 把第一個元素加入緩衝區
        Pair buffer = new Pair(nums[0], 1);
        for(int i = 1; i < nums.length; ++i)
        {
            // 如果緩衝區爲空
            if(buffer.getN() == 0)
            {
                buffer.setNumber(nums[i]);
                buffer.addOne();
            }
            // 如果當前讀入元素與緩衝區相等
            else if(buffer.equals(nums[i]))
            {
                // 計數加1
                buffer.addOne();
            }
            else
            {
                // 計數減1
                buffer.minusOne();
            }
        }
        return buffer.getNumber();
    }

    // 定義一個類來代表緩衝區
    class Pair
    {
        private int number; // 數字
        private int n;  // 數量

        public Pair(int number, int n)
        {
            this.number = number;
            this.n = n;
        }

        public void setNumber(int number)
        {
            this.number = number;
        }

        public int getNumber()
        {
            return this.number;
        }

        public int getN()
        {
            return n;
        }

        public void addOne()
        {
            this.n++;
        }

        public void minusOne()
        {
            this.n--;
        }

        public boolean equals(int n)
        {
            return number == n;
        }

    }
}
發佈了79 篇原創文章 · 獲贊 58 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章