亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

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

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网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美精品999| 国产一区二区三区久久精品| 97在线观看视频国产| 亚洲伊人一本大道中文字幕| 国产伦精品一区二区三区精品视频| 92版电视剧仙鹤神针在线观看| 成人激情视频网| 亚洲精品资源美女情侣酒店| 55夜色66夜色国产精品视频| 精品呦交小u女在线| 九九九热精品免费视频观看网站| 午夜精品久久久久久99热| 麻豆国产va免费精品高清在线| 久久99久久99精品中文字幕| 亚洲精品美女在线| 欧美一级视频在线观看| 全亚洲最色的网站在线观看| 亚洲高清免费观看高清完整版| 国产精品一区二区三区免费视频| 久久综合五月天| 欧美精品制服第一页| 日韩av观看网址| 日本a级片电影一区二区| 欧美中文在线观看国产| 国产精品成人一区二区| 久久久之久亚州精品露出| 亚洲伊人第一页| 久久久亚洲精品视频| 国产这里只有精品| 中文字幕日韩精品有码视频| 国产乱肥老妇国产一区二| 国产精品福利久久久| 国产精品xxx视频| 日本高清视频一区| 成人激情免费在线| 亚洲伊人久久大香线蕉av| 国产一区二区动漫| 91在线观看免费| 欧美电影《睫毛膏》| 国产精品女人网站| 欧美日韩国产影院| 国产日韩欧美视频在线| 91在线色戒在线| 欧美视频13p| 欧美大片免费观看在线观看网站推荐| 人九九综合九九宗合| 国产精品一区二区三区在线播放| 中文字幕日韩欧美精品在线观看| 国产精品旅馆在线| 国外成人在线视频| 欧美激情亚洲激情| 亚洲综合成人婷婷小说| 日韩精品免费视频| 美乳少妇欧美精品| 久久久久久久久电影| 免费不卡欧美自拍视频| 欧美视频在线免费| 日韩中文字幕在线精品| 亚洲综合中文字幕在线| 亚洲人成网7777777国产| 国产亚洲一区二区精品| 久久天天躁日日躁| 6080yy精品一区二区三区| 国产精品久久电影观看| 欧美孕妇性xx| 庆余年2免费日韩剧观看大牛| 欧美激情性做爰免费视频| 成人国产精品色哟哟| 亚洲欧美在线一区| 丝袜亚洲欧美日韩综合| 亚洲国产成人精品电影| 在线视频亚洲欧美| 亚洲免费视频在线观看| 国产剧情日韩欧美| 亚洲精品日韩在线| 亚洲精品国产精品国自产观看浪潮| 国产乱人伦真实精品视频| 国产成一区二区| 久久久久久久久久av| 欧美一级黑人aaaaaaa做受| 欧美精品18videos性欧美| 亚洲激情视频在线播放| 国产精品欧美亚洲777777| www亚洲精品| 精品无码久久久久久国产| 92版电视剧仙鹤神针在线观看| 中文字幕最新精品| 国产一区二区黑人欧美xxxx| 亚洲风情亚aⅴ在线发布| 欧美高清在线视频观看不卡| 久久久久999| 91精品国产高清自在线| 日韩视频免费中文字幕| 亚洲精品ady| 久久久精品影院| 精品国产91乱高清在线观看| 8050国产精品久久久久久| 久久久国产一区| 岛国av一区二区三区| 久久琪琪电影院| 欧美高清videos高潮hd| 日韩欧美精品免费在线| 亚洲自拍偷拍在线| 亚洲色图欧美制服丝袜另类第一页| 亚洲电影第1页| 欧美成人h版在线观看| 欧美成人精品三级在线观看| 国产精品流白浆视频| 欧美日韩性视频在线| 亚洲天堂开心观看| 亚洲国产精品嫩草影院久久| 在线观看精品自拍私拍| 亚洲激情成人网| 日本精品久久久久久久| 午夜精品福利视频| 国产精品久久av| 在线视频国产日韩| 国产精品女主播视频| 久久久久久噜噜噜久久久精品| 色综合视频一区中文字幕| 成人午夜在线观看| 成人网在线免费观看| 欧美一级电影免费在线观看| 一区二区三区黄色| 2019亚洲男人天堂| 国产在线拍揄自揄视频不卡99| 国产激情久久久| 国产精品国产三级国产aⅴ浪潮| 欧美成人久久久| 亚洲男人天堂九九视频| 亚洲视频精品在线| 亚洲欧美日韩天堂| 亚洲欧美日韩爽爽影院| 欧美日韩精品二区| 亚洲人午夜色婷婷| 久久91精品国产91久久跳| 日韩精品久久久久久久玫瑰园| 精品视频www| 亚洲欧美成人在线| 91精品免费视频| 亚洲国产欧美一区| 国产精品亚洲片夜色在线| 国内精品视频一区| 91精品国产91久久久久久吃药| 亚洲欧美制服综合另类| 在线观看精品自拍私拍| 亚洲一区二区少妇| 亚洲欧美日韩中文视频| 国产欧美一区二区三区久久人妖| 国产精品免费在线免费| 欧美精品videosex性欧美| 国产精品欧美一区二区三区奶水| 亚洲a级在线观看| 亚洲人成网站999久久久综合| 亚洲国产91色在线| 欧美激情亚洲自拍| 亚洲精品自拍第一页| 高潮白浆女日韩av免费看| 国产一区二区美女视频| 亚洲精品小视频| 91精品久久久久久久久| 国产精品成人aaaaa网站| 日本精品久久久久久久| 国产一区二区三区在线观看视频|