169 & 229. Majority Element & Majority Element II

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.

/**
 * @param {number[]} nums
 * @return {number}
 */
var majorityElement = function(nums) {
    var num = nums.length;
    var count = 0;
    var item;
    for (var i = 0; i<num; i++) {
        if (count === 0) {
            item = nums[i];
        }
        if (nums[i]===item) 
            count++;
        else
            count--;
    }
    return item;
};

我們從數(shù)組第一個(gè)開(kāi)始找起
如果當(dāng)前候選人的計(jì)數(shù)是0,就把當(dāng)前這個(gè)元素當(dāng)成候選人
如果當(dāng)前這個(gè)元素和候選人相同,候選人計(jì)數(shù)就加1,不同就減一,如果減至0,就意味著當(dāng)前候選人在之前的元素中被抵消完了,在已經(jīng)檢查過(guò)的元素中,該候選人不滿足大于n/2的條件。

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.
現(xiàn)在要求找到個(gè)數(shù)大于n/3的,這樣的數(shù)應(yīng)該最多只有兩個(gè)。我們拓展一下剛才的方法,使用兩個(gè)指針

var majorityElement = function(nums) {
    var num = nums.length;
    if (num === 0)
        return [];
    var item1,item2;
    var c1 = 0;
    var c2 = 0;
    for (let i = 0;i < num;i++) {
        //如果C1和C2都是0,優(yōu)先給C1
        if (c1 === 0&&c2 === 0) {
            item1 = nums[i];
            c1++;
        //如果C1不是0,C2是0
        } else if (c1 !== 0&&c2 === 0) {
            //檢查是不是C1該記錄的元素
            //是就分給C1,不是就給C2
            if (item1 === nums[i]) {
                c1++;
            } else {
                item2 = nums[i];
                c2++;
            }
        //同理
        } else if (c1 === 0&&c2 !== 0) {
            if (item2 === nums[i]) {
                c2++;
            } else {
                item1 = nums[i];
                c1++;
            }
        //如果C1,C2此時(shí)都有分配,那檢查當(dāng)前元素屬于誰(shuí)
        } else {
            if (item2 === nums[i]) {
                c2++;
            } else if (item1 === nums[i]) {
                c1++;
            //如果誰(shuí)都不屬于,那就都減
            } else {
                c1--;
                c2--;
            }
        }
        //更簡(jiǎn)單的判斷方法,思想其實(shí)一樣的
        // if (item1 === nums[i]) {
        //     c1++;
        // } else if (item2 === nums[i]) {
        //     c2++;
        // } else if (c1 === 0) {
        //     item1 = nums[i];
        //     c1++;
        // } else if (c2 === 0) {
        //     item2 = nums[i];
        //     c2++;
        // } else {
        //     c1--;
        //     c2--;
        // }
    }
    c1 = 0;
    c2 = 0;
    //現(xiàn)在只能說(shuō)item1和2是出現(xiàn)次數(shù)最多的
    //找到當(dāng)前item1和item2出現(xiàn)的總次數(shù),看看是不是符合題目的要求
    for (let i = 0; i < num; i++) {
        if (nums[i] == item1)
            c1++;
        else if (nums[i] == item2)
            c2++;
    }
    var res = [];
    if (c1 > num / 3)
        res.push(item1);
    if (c2 > num / 3)
        res.push(item2);
    return res;
};
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問(wèn)題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,922評(píng)論 0 33
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,121評(píng)論 0 23
  • 1.簽到指引 2.活動(dòng)的分工 3.徐姐的月餅制作講解,要進(jìn)行研討,話術(shù)進(jìn)行規(guī)范。 4.安全處理 5.思考活動(dòng)游戲化...
    亦張亦合閱讀 361評(píng)論 0 0
  • 敲窗夜雨,慘淡西風(fēng),檐頭淅瀝起處。悱惻難眠燈影,獨(dú)寥無(wú)語(yǔ)。寒簾懶怯不卷,似此時(shí)、五更分許。別昨夢(mèng),起閑愁,卻道幾番...
    姑射閱讀 377評(píng)論 1 2
  • 前期準(zhǔn)備 swiper swiperAnimate插件 swiperAnimate配置文件js手寫(xiě) 布局rem v...
    ZoeDu閱讀 331評(píng)論 0 0

友情鏈接更多精彩內(nèi)容