【leetcode 哈希表】Majority Element

【leetcode 哈希表】Majority Element

@author:wepon

@blog:http://blog.csdn.net/u012162613

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.

2、分析

题意:给定一个长度为n的数组,找出majority element,所谓majority element就是出现次数大于n/2的那个数。

很简单的题目,解法很多:

  1. Runtime: O(n2) — Brute force solution: Check each element if it
    is the majority element.
  2. Runtime: O(n), Space: O(n) — Hash table: Maintain a hash table of the counts of each element, then find the most common one.
  3. Runtime: O(n log n) — Sorting: Find the longest contiguous identical element in the array after sorting.
  4. Average runtime: O(n), Worst case runtime: Infinity — Randomization: Randomly pick an element and check if it is the majority element. If it is not, do the random pick again until you find
    the majority element. As the probability to pick the majority element is greater than 1/2, the expected number of attempts is < 2.
  5. Runtime: O(n log n) — Divide and conquer: Divide the array into two halves, then find the majority element A in the first half and the majority element B in the second
    half. The global majority element must either be A or B. If A == B, then it automatically becomes the global majority element. If not, then both A and B are the candidates for the majority element, and it is suffice to check the count of occurrences for at
    most two candidates. The runtime complexity, T(n) = T(n/2) + 2n = O(n log n).
  6. Runtime: O(n) — Moore voting algorithm: We maintain a current candidate and a counter initialized to 0. As we iterate the array, we look at the current element x:
    1. If the counter is 0, we set the current candidate to x and the counter to 1.
    2. If the counter is not 0, we increment or decrement the counter based on whether x is the current candidate.

    After one pass, the current candidate is the majority element. Runtime complexity = O(n).

  7. Runtime: O(n) — Bit manipulation: We would need 32 iterations, each calculating the number of 1‘s for the ith bit
    of all n numbers. Since a majority must exist, therefore, either count of 1‘s > count of 0‘s or vice versa (but can never be equal). The majority number’s ith bit
    must be the one bit that has the greater count.

下面给出hash table的代码,其他的有空再写到評論里吧。

3、代码

 int majorityElement(vector<int> &num) {
        unordered_map<int,int> map;
        int len=num.size();
        for(int i=0;i<len;i++){
            if(map.find(num[i])==map.end()) map[num[i]]=0;
            else   map[num[i]]++;

            if(map[num[i]]>=len/2) return num[i];
        }
    }
时间: 2024-10-09 02:05:30

【leetcode 哈希表】Majority Element的相关文章

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

题目 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 t

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的数组,找到主要的元素.

[leetcode]Divide and Conquer-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:Special thanks to @t

Leetcode哈希表算法

LeetCode-python 1.两数之和 难度:简单       类型: 哈希表 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例 给定 nums = [2, 7, 11, 15], target = 9因为 nums[0] + nums[1] = 2 + 7 = 9所以返回 [0, 1] 解题思路 用字典保存遍历过的数字和下标寻找

Map - leetcode [哈希表]

149. Max Points on a Line unordered_map<float, int> hash 记录的是斜率对应的点数 unordered_map<float, int>::iterator it = hash.begin(); it != hash.end(); it++ 斜率k = (y2 - y1) / (x2 - x1) 最后记得加上duplicate的点数

[LeetCode][JavaScript]Majority Element

https://leetcode.com/problems/majority-element/ 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 majorit

【STL】哈希表 uthash.h

散列表(Hash table,也叫哈希表),是根据关键字(Key value)而直接访问在内存存储位置的数据结构.线性表查找的时间复杂度为O(n)而平衡二叉树的查找的时间复杂度为O(log(n)).无论是采用线程表或是树进行存储,都面临面随着数据量的增大,查找速度将不同程度变慢的问题.而哈希表正好解决了这个问题. 给定表M,存在函数f(key),对任意给定的关键字值key,代入函数后若能得到包含该关键字的记录在表中的地址,则称表M为哈希(Hash)表,函数f(key)为哈希(Hash) 函数 函

[LeetCode] 229. Majority Element II 多数元素 II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The algorithm should run in linear time and in O(1) space. Example 1: Input: [3,2,3] Output: [3] Example 2: Input: [1,1,1,3,3,2,2,2] Output: [1,2] 169. Maj