麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

Leetcode: Majority Element

2019-11-14 22:19:14
字體:
來源:轉載
供稿:網友
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的官方答案給的解答很好,我的方法是HashMap. 除了brute force和sorting常見方法以外,還有幾個方法,思路都還不錯,1是我的方法,我覺得2、4、5都是不錯的思路。

  1. Runtime: O(n), Space: O(n) — Hash table: Maintain a hash table of the counts of each element, then find the most common one.
  2. 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.
  3. Runtime: O(nlogn) — 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(nlogn).
  4. 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).
  5. Runtime: O(n) — Bit manipulation: We would need 32 iterations, each calculating the number of 1's for the ithbit of allnnumbers. 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 ithbit must be the one bit that has the greater count.
 1 public class Solution { 2     public int majorityElement(int[] num) { 3         int n = num.length; 4         HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); 5         for (int elem : num) { 6             if (map.containsKey(elem)) { 7                 map.put(elem, map.get(elem)+1); 8             } 9             else {10                 map.put(elem, 1);11             }12         }13         for (int item : map.keySet()) {14             if (map.get(item) > n/2) {15                 return item;16             }17         }18         return -1;19     }20 }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 一区二区三区在线观看国产 | av在线一区二区三区四区 | 亚洲特黄a级毛片在线播放 久久久入口 | 看黄在线观看 | 久久国产乱子伦精品 | 男女生羞羞视频网站在线观看 | 黄色电影免费网址 | 成人免费av在线 | 久久精品观看 | 黄色网址在线免费 | 免费在线观看毛片视频 | 久久亚洲视频网 | 手机在线看片国产 | 久久久一区二区三区视频 | 福利在线免费视频 | 欧美日韩爱爱视频 | 小视频免费在线观看 | 欧美一级高潮 | 一区国产在线 | 精品亚洲福利一区二区 | 欧美一级淫片a免费播放口 91九色蝌蚪国产 | 免费观看三级毛片 | 男女生羞羞视频网站在线观看 | 女人叉开腿让男人桶 | 成人精品aaaa网站 | 九一免费版在线观看 | 本站只有精品 | 99国语露脸久久精品国产ktv | 色妇视频| 欧美精品久久久久久久久久 | 国产视频第一区 | 欧美一级精品片在线看 | 蜜桃91丨九色丨蝌蚪91桃色 | 天天干干 | 成人羞羞国产免费游戏 | 性欧美性欧美 | 一区二区三区视频在线观看 | 成人在线观看免费 | 午夜视频中文字幕 | 99在线啪| av久草 |