Majority Element II
https://leetcode.com/problems/majority-element-ii/#/description
挑出所有大于n/3 的数,两个很相似的题,但是这次的major 是> n/3 而且是要挑出‘所有’
hash 表和排序依然可以,但是题目要求O(n) 就没办法了。
majority vote 的修改版:这个算法是一次投两个数而不是一个,这样我们可以投出两个major,而因为是n/3,所以最多只有两个major。
故函数返回要么是空,要么有1 个元素,要么有2 个。用majority vote 算法vote 出两个major 但是却不能保证他们大于n/3,所以要再遍历一次,确保>n/3 以后再添加到返回结果里。
var majorityElement = function(nums) {var t1, t2, n1 = 0, n2 = 0;var len = nums.length;for (var i = 0; i < len; i++) {if (t1 === nums[i]) {n1++;continue;}if (t2 === nums[i]) {n2++;continue;}if (n1 === 0) {t1 = nums[i];n1 = 1;continue;}if (n2 === 0) {t2 = nums[i];n2 = 1;continue;}n1--;n2--;}var z1 = 0, z2 = 0;for (var i = 0; i < len; i++) {if (n1 > 0 && nums[i] === t1) {z1++;continue;}if (n2 > 0 && nums[i] === t2) {z2++;continue;}}var ret = [];if (z1 > len / 3) ret.push(t1);if (z2 > len / 3) ret.push(t2);return ret; }
转载于:https://www.cnblogs.com/agentgamer/p/7019837.html
Majority Element II相关推荐
- Majority Element(169) Majority Element II(229)
寻找多数元素这一问题主要运用了:Majority Vote Alogrithm(最大投票算法) 1.Majority Element 1)description Given an array of s ...
- [LeetCode] Majority Element II
Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...
- LeetCode 229 : Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- LeetCode Majority Element II(Moore Voting Algorithm即Majority Voting Algorithm)
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algo ...
- leetcode 229. Majority Element II(多数投票算法)
就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public:vector<int&g ...
- 47 Majority Element II
原题网址; https://www.lintcode.com/problem/majority-element-ii/ 描述 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三 ...
- 229. Majority Element II**
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- [leetcode] 229. Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- 229. Majority Element II 【M】【52】
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
最新文章
- 在WEBSERVICE学习中遇到的问题
- python3 multiprocessing 多进程 列表类型 listproxy 清除内容
- A. Boredom【DP】
- 解决git bash右键菜单消失的问题
- javaScript中的回调函数
- 重置密码解决MySQL for Linux错误 ERROR 1045 (28000):
- Vue.js 2.0版
- TED如何和压力做朋友(第四天完)
- su室内插件_SketchUp必学插件TOP100,每一个都是[室内/装饰/景观]设计的效率神器...
- ffmpeg命令分析-vf
- MOS管、IGBT、BJT的区别
- 【Python数据分析】<数据分析工具>基于Excel的数据分析
- python列表创建操作_python列表操作
- Python实现Reed-Xiaoli(RX)高光谱目标检测算法
- CAD格式刷怎么用?CAD格式刷使用技巧
- 项目中使用docker部署xxl-job
- 2013-2019年百度搜索算法规则解读与应对
- Max-Margin Regularization for Chamfer Matching
- 【网络摘录】Google网页搜索技巧
- ygomobile卡组下载网站_YGOMobile:用手机也能免费玩游戏王
热门文章
- [UE4]自动旋转组件
- flask-wtforms
- 基础 - jQuery选项卡
- 返回多个不同类型参数的值(out)可变参数一维数组( params )把值从方法中带出(ref) 方法递归(方法自己调用自己与循坏类似注意return每次只能跳出一个方法))...
- adult道具项目开发 - 模式
- VIM配置(不用这么麻烦了,文章尾部附带他人写得脚本安装)
- android NDK 知识汇总
- 使用共享网口方式访问BMC管理控制器
- Oracle分析函数三——SUM,AVG,MIN,MAX,COUNT
- [转]你所不知的 CSS ::before 和 ::after 伪元素用法