[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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章