Time:2019/4/5
Title: Majority Element 2
Difficulty: medium
Author: 小鹿javascript
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋
times.java
Note: The algorithm should run in linear time and in O(1) space.git
Example 1:github
Input: [3,2,3] Output: [3]
Example 2:算法
Input: [1,1,1,3,3,2,2,2] Output: [1,2]
一、上一篇是一个求超过 n/2 个数的数,采起的是用一个计数变量和计数元素变量来完成的。本篇题目是超过 n/3 的个数, 咱们分别用两个变量来完成, 条件限制是时间复杂度是O(n),空间复杂度为O(1)。二、继续使用摩投票算法,假设要投票,选取票数超过 n/3 以上选为候选人,一个 n 被分为三等份,也就是说最多有两位当选人。编程
三、假设第一我的投甲,第二个投乙,甲、乙都站到台上各个计数为 1 ,随之第三个投甲,第四个投乙,计数分别加 1 ,第五个投丙,由于只有两个台子,因此那就让甲乙票数计数各减 1 ,当甲、乙的票数分别减到 0 以后,第 m 我的投丙,就让丙代替甲或已,继续遍历数据,直到遍历完成为止。app
四、上述只选择出了那些有可能知足条件的数据,下面对cm、cn存储的数据作验证。测试
若是没有题目中时间复杂度和空间复杂度的限制,有多种解决方法:一、散列表解决:先遍历一遍数据,统计每一个数字出现的次数,而后再遍历一遍散列表,找出知足条件的数字。时间复杂度为O(n),空间复杂度为 O(n)。spa
二、排序解决:先进行一次排序(快排),而后遍历数据,找出知足条件的数据。时间复杂度为O(nlogn),空间复杂度为O(1)。code
/** * @param {number[]} nums * @return {number[]} */ var majorityElement = function(nums) { let [m,n,cm,cn,countm,countn] = [0,0,0,0,0,0]; let result = []; for(let i = 0;i < nums.length; ++i){ //m === nums[i]和n === nums[i]的判断必定放到 cm === 0 和 cn === 0 以前,负责产生 bug。 if(m === nums[i]){ cm ++; }else if(n === nums[i]){ cn ++; }else if(cm === 0){ m = nums[i]; cm ++; }else if(cn === 0){ n = nums[i]; cn ++; }else{ cn--; cm--; } } //验证 cm、cn 存储的数据 for(let index in nums){ if(nums[index] === m){ ++countm; } if(nums[index] === n){ ++countn; } } if(countm > Math.floor(nums.length/3)){ result.push(m); } if(countn > Math.floor(nums.length/3) && n != m){ result.push(n); } return result; }; //测试 let arr =[1,2,2,3,2,1,1,3]; console.log(majorityElement(arr));
欢迎一块儿加入到 LeetCode 开源 Github 仓库,能够向 me 提交您其余语言的代码。在仓库上坚持和小伙伴们一块儿打卡,共同完善咱们的开源小仓库!
Github:https://github.com/luxiangqia...