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.

大意:

给出一个尺寸为n的数组,找到主要的元素。所谓主要的元素是指出现次数超过n/2的元素。

你可以假设数组不为空且主要元素一定存在。

思路:

第一直觉是先排序把相同的元素都放到一起再说,因为主要元素的出现次数大于n/2,那么排序后最中间的元素一定是主要元素,不管怎么移动位置,最中间的都一定是它,所以可以很简单地完成代码啦。

代码(Java):

public class Solution {
    public int majorityElement(int[] nums) {
        Arrays.sort(nums);// 先排序
        return nums[nums.length/2];// 出现次数超过n/2次的元素排序后一定会出现在中间
    }
}

他山之石:

现在让我们看看Discuss最hot的答案,我的做法并不是最快的,因为排序需要时间,他说他的时间复杂度为O(1),看了一下代码,他的做法是:用一个变量记录主要元素,初始化为第一个元素,一个变量记录出现次数,初始化为1,遍历数组中的元素,与当前记录的主要元素相同的话,次数就加1,不同就减1,如果次数减到0,那就将主要元素换成新遍历到的元素,这样遍历完一轮得到最后记录的主要元素,就是我们要的结果。因为主要元素出现的次数大于n/2,所以可以想见最后留下来的一定会是主要元素。别的元素即使记录过也会因为次数归零抛弃掉的。这个方法只需要遍历一次数组就可以了,确实不容易想到。代码如下:

public class Solution {
    public int majorityElement(int[] num) {

        int major=num[0], count = 1;
        for(int i=1; i<num.length;i++){
            if(count==0){
                count++;
                major=num[i];
            }else if(major==num[i]){
                count++;
            }else count--;

        }
        return major;
    }
}

版权所有:http://blog.csdn.net/cloudox_

时间: 2024-10-09 02:05:36

LeetCode笔记:169. Majority Element的相关文章

【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. 题解: Solution 1 () class

leetcode笔记: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. 二. 题目分析 题目说到,给定一个数

[LeetCode] NO. 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. [题目解析] 根据题目需要求数组中,出现次数过

【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. Credits: 自己的思路大概是建立一个map,

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. 思路.1 排序,选择第n/2个数,调用STL的sort,

[Lintcode]46. Majority Element/[Leetcode]169. Majority Element

46. Majority Element/[169. Majority Element(https://leetcode.com/problems/majority-element/) 本题难度: Easy Topic: Greedy Description Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find

LeetCode Javascript实现 169. Majority Element

169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = function(nums) { var hash = {}; var y=-1,z; //注意这里的方括号,利用变量访问对象属性时要用方括号 for(var i=0;i<=nums.length-1;i++){ if(hash[nums[i]]){ hash[nums[i]]++; }else{ hash[

leetCode 169. Majority Element 数组

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. 思路1: 使用m

169. Majority Element - LeetCode

Question 169. Majority Element Solution 思路:构造一个map存储每个数字出现的次数,然后遍历map返回出现次数大于数组一半的数字. 还有一种思路是:对这个数组排序,次数超过n/2的元素必然在中间. Java实现: public int majorityElement(int[] nums) { Map<Integer, Integer> countMap = new HashMap<>(); for (int num : nums) { In

Leetcode#169. Majority Element(求众数)

题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ? n/2 ? 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] 输出: 3 示例 2: 输入: [2,2,1,1,1,2,2] 输出: 2 思路 思路一: 利用哈希表的映射,储存数组中的数字以及它们出现的次数,当众数出现时,返回这个数字. 思路二: 因为众数是出现次数大于n/2的数字,所以排序之后中间的那个数字一定是众数.即nums[n/2]为众数.但是在计算比