https://oj.leetcode.com/problems/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:

Special thanks to @ts for adding this problem and creating all test cases.

Show Tags

public class Solution {
    public int majorityElement(int[] num) {
        int last=num[0];
        int cnt=1;
        for(int i=1;i<num.length;i++){
            if(num[i]!=last){
                cnt--;
                if(cnt==0){
                    last=num[i];
                    cnt=1;
                }
            }else{
                cnt++;
            }
        }
        return last;
    }
}
时间: 2024-08-03 17:59:36

https://oj.leetcode.com/problems/majority-element/的相关文章

【LeetCode 229】Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. 思路: [LeetCode 169]Majority Element 的拓展,这回要求的是出现次数超过三分之一次的数字咯,动动我们的大脑思考下,这样的数最多会存在几个呢,当然是2个嘛.因此,接着上一题的方

LeetCode题目:Majority Element

这同样也是一道LeetCode Online Judge上面的题目,属于easy级别,原题如下: 题目 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 alw

LeetCode Array Easy169. Majority Element

Description 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

【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. 思路: 找到一个数组中出现次数超过一半的数.排序.哈希等

Leetcode # 169, 229 Majority Element I and II

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】229. Majority Element II

Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. Hint: How many majority elements could it possibly have? Do you have a better hint