[leedcode 229] 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.
public class Solution {public List<Integer> majorityElement(int[] nums) {/*首先,可以通过分析得到结论:满足条件的数字个数cnt最多为2。 证明: ifcnt>2⇒cnt× (⌊n/3⌋+1 )>n 超出原数组的大小。 然后,借鉴在数组中求出现次数超过一半的数这道题的思路: 1). 第一遍扫描,设两个计数器和变量记录数组nums[]中出现频率最高的数。 2). 第二遍扫描,计算着两个数出现的次数。 3). 判断这两个数是否符合要求,符合则存入结果集。需要注意的是:第一个for循环的判断顺序,首先要判断是否等于m或n*/List<Integer> res=new ArrayList<Integer>();if(nums==null||nums.length<=0) return res;int m=0;int n=0;int countm=0;int countn=0;for(int i=0;i<nums.length;i++){/*if(m==nums[i]) countm++;else if(n==nums[i]) countn++;else if(countm==0){countm=1;m=nums[i];}else if(countn==0){countn=1;n=nums[i];}else {countm--;countn--;}*/if(m==nums[i]||countm==0){countm++; m=nums[i];}else if(n==nums[i]||countn==0){countn++; n=nums[i];}else{countm--;countn--;}}countm=0;countn=0;for(int i=0;i<nums.length;i++){if(nums[i]==m) countm++;else if(nums[i]==n) countn++;}if(countm>nums.length/3) res.add(m);if(countn>nums.length/3) res.add(n);return res;} }
转载于:https://www.cnblogs.com/qiaomu/p/4713306.html
[leedcode 229] Majority Element II相关推荐
- 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 229. Majority Element II(多数投票算法)
就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public:vector<int&g ...
- 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 ...
- 229. Majority Element II
class Solution(object): def majorityElement(self, nums): """ :type nums: List[int] :r ...
- leetcode 229. Majority Element II | 229. 求众数 II(找出现次数超过n/k的元素)
题目 https://leetcode.com/problems/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 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 ...
最新文章
- poj3686(最小费用流)
- 服务器重装ie浏览器,服务器内部IE浏览器或相关软件访问支付宝异常问题解决方案...
- 利用libevent 和线程池实现高并发服务器的设计
- 八句经典座右铭必有一句适合你
- 安卓学习 之 网络技术(十)
- java-多线程-一道阿里面试题分析
- js 定位当前城市之接口定位(搜狐、新浪、百度、腾讯API)
- MySQL详细教程 这一篇就够啦!
- 音频插件使用(wavesurfer.js)
- 读WebTrends的Javascript源码笔记
- win7系统服务器修改文件权限,Windows7系统修改Trustedinstaller权限文件的方法
- 计算机课word实验总结,大学计算机操作实践报告【实验8】Word2010综合实验
- 3G中的A-GPS移动定位技术
- Linux 内核 notifier机制
- 使用three.js做一个网页的相册动画
- 企业微信CRM管理系统的优势
- node安装以及nvm管理node版本
- 学习SAR和INSAR的一点随笔
- windows截图快捷键方法
- [ubuntu20]查看是否是固态硬盘
热门文章
- wp转shp_将china.wl拓扑生成的wp面转换成shp格式,然后在arcgis中打开,截图,提交截图。...
- javascript遍历对象的所有的属性
- python tk combobox设置值为空_在PDMS中使用python直接生成管口方位图(开源分享第二集)...
- Debug日志正常,输出和HTML页面乱码
- 血汗泪计算机音乐,血汗泪 - pikaCinDy_ - 5SING中国原创音乐基地
- 6.1倒计时锁(Countdown Latches)
- SpringMVC的RESTful(一)
- 【渝粤题库】 陕西师范大学 202331 证券投资学 作业(专升本)
- 论文笔记:Semantic Relation Reasoning for Shot-Stable Few-Shot Object Detection
- 概率矩阵分解模型 PMF