Java-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.

要求找出出现次数过半的数组元素 只要使用哈希表 进行一次遍历存储 满足hm.get(num[i])>num.length/2-1的即使所求结果 代码如下:

public class Solution {
    public int majorityElement(int[] num) {
        Map<Integer,Integer> hm=new HashMap<Integer,Integer>();
        for(int i=0;i<num.length;i++){
            if(hm.containsKey(num[i])){
                if(hm.get(num[i])>num.length/2-1){
                    return num[i];
                }else{
                    hm.put(num[i],hm.get(num[i])+1);
                }
            }else{
                    hm.put(num[i],1);
            }
        }
        return num[0];
    }
}
时间: 2024-10-12 13:02:00

Java-Majority Element的相关文章

Java for 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. 解题思路: 编程之美P130(寻找发帖水王)原题,如果删

leetcode 169. Majority Element 求出现次数最多的数 --------- java

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. 给定一个数组,求其中权制最大的元素,(该元素出现超过了一

169 Majority Element [LeetCode Java实现]

题目链接: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——Majority Element

在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素.容易想到的方式就是计数,出现次数最多的就是majority element,其次就是排序,中间的就是majority element.但是还有两种更有意思的实现方式时间效率O(n),空间效率O(1):1.Moore voting algorithm 投票算法,因为符合要求的majority element总是存在的,所以首先置计数器count=1,并选择数组的第一个元素作为candidate,往后遍历并计数,与candidate相

LeetCode OJ 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. Hint: How many majority elements could it possibly have? Do you have a better hint? Suggest it! [题目分析]

【10_169】Majority Element

今天遇到的题都挺难的,不容易有会做的. 下面是代码,等明天看看Discuss里面有没有简单的方法~ Majority Element My Submissions Question Total Accepted: 79833 Total Submissions: 208075 Difficulty: Easy Given an array of size n, find the majority element. The majority element is the element that

C 语言构造hash table 解 LC majority element问题

Leetcode上 majority element这题是 有 时间O(N), 空间O(1)的解的. https://leetcode.com/problems/majority-element/ 用hash table来解则为 时间O(N), 空间O(N). 如果是Java里 用HashMap很方便了. 有位同学问怎么用c语言来构造hash table. 我就随手写了一个: typedef struct Node { int val, count; } Node; typedef struct

Majority Element 解答

Solution 1 Naive way First, sort the array using Arrays.sort in Java. Than, scan once to find the majority element. Time complexity O(nlog(n)) 1 public class Solution { 2 public int majorityElement(int[] nums) { 3 int length = nums.length; 4 if (leng

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. 题目标签: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. 大意: 给出一个尺寸为n的数组,找到主要的元素.