• [LeetCode] 169. Majority Element


    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.

    Example 1:

    Input: [3,2,3]
    Output: 3

    Example 2:

    Input: [2,2,1,1,1,2,2]
    Output: 2

    求众数。题意是给一个数组,有一个数字出现次数超过了数组长度的一半,请求出这个数字。

    我给出几个不同解法,其中最优解投票法。

    1. 排序,然后直接找数组中间那个数字。

    时间O(nlogn)

    空间O(1)

    1 /**
    2  * @param {number[]} nums
    3  * @return {number}
    4  */
    5 var majorityElement = function(nums) {
    6     nums.sort();
    7     return nums[Math.floor(nums.length / 2)];
    8 };

    2. hashmap计算每个不同元素出现的次数,返回次数超过数组长度一半的那个数字。

    时间O(n)

    空间O(n)

     1 /**
     2  * @param {number[]} nums
     3  * @return {number}
     4  */
     5 var majorityElement = function(nums) {
     6     let dict = {};
     7     let breakpoint = nums.length / 2;
     8     for (let i = 0; i < nums.length; i++) {
     9         dict[nums[i]] = dict[nums[i]] || 0;
    10         dict[nums[i]]++;
    11         if (dict[nums[i]] > breakpoint) {
    12             return nums[i];
    13         }
    14     }
    15 };

    3. 投票法。意思是先assume数组的第一个元素是要找的元素,设为candidiate,再记录一个变量count。遍历数组,如果遍历到跟X不同的数字的时候,count--,当count == 0的时候,同时需要换一个candidate;如果跟X一样,就count++。最后剩下的那个candidate就是要找的元素,因为他的出现次数超过了数组的一半所以一定不会被替换掉。

    时间O(n)

    空间O(1)

    JavaScript实现

     1 /**
     2  * @param {number[]} nums
     3  * @return {number}
     4  */
     5 var majorityElement = function (nums) {
     6     let count = 0;
     7     let res = 0;
     8     let candidate = nums[0];
     9     for (let num of nums) {
    10         if (num === candidate) {
    11             count++;
    12         } else {
    13             count--;
    14         }
    15         if (count === 0) {
    16             candidate = num;
    17             count++;
    18         }
    19     }
    20     return candidate;
    21 };

    Java实现

     1 class Solution {
     2     public int majorityElement(int[] nums) {
     3         int count = 0;
     4         int candidate = nums[0];
     5         for (int num : nums) {
     6             if (num != candidate) {
     7                 count--;
     8             } else {
     9                 count++;
    10             }
    11             if (count == 0) {
    12                 candidate = num;
    13                 count++;
    14             }
    15         }
    16         return candidate;
    17     }
    18 }

    相关题目

    169. Majority Element

    229. Majority Element II

    LeetCode 题目总结

  • 相关阅读:
    计算机网络 基础 1
    JAVA 基础之 多线程
    HashMap 多线程处理之 FailFast机制:
    Struts2
    JAVA 由浅及深之 Servlet
    Servlet 会话技术 , Session 及 Cookie 详解
    JAVA 设计模式 : 单例模式
    JAVA 基础之 序列化 Serializable
    代理模式 及 实现AOP 拦截机制
    web.xml 文件详解 及 listener、 filter、servlet 加载顺序
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12237347.html
Copyright © 2020-2023  润新知