Majority Element II——LeetCode
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.
题目大意:给定一个大小为n的数组,找出Majority元素,满足出现次数大于 ⌊ n/3 ⌋
次。
解题思路:大于 ⌊ n/3 ⌋
次,那么应该最多有两个Majority元素,之前有道题是找出 大于⌊ n/2 ⌋
次Majority元素,这道题有点类似,稍微复杂一点,还是利用投票算法,count作为对元素的计数,候选元素等于当前元素则count+1,否则减1,count等于0则更换候选元素。
public List<Integer> majorityElement(int[] nums) {
List<Integer> res = new ArrayList<>();
if (nums == null || nums.length == 0) {
return res;
}
int can1 = nums[0], can2 = nums[0], cnt1 = 1, cnt2 = 0;
for (int i = 1; i < nums.length; i++) {
int num = nums[i];
if ((cnt1 == 0 && num != can2)) {
can1 = num;
} else if (cnt2 == 0 && num != can1) {
can2 = num;
}
if (num == can1) {
cnt1++;
} else if (num == can2) {
cnt2++;
} else {
cnt1--;
cnt2--;
}
cnt1 = cnt1 < 0 ? 0 : cnt1;
cnt2 = cnt2 < 0 ? 0 : cnt2;
}
cnt1 = 0;
cnt2 = 0;
for (int num : nums) {
if (num == can1) {
cnt1++;
} else if (num == can2) {
cnt2++;
}
}
if (cnt1 > nums.length / 3)
res.add(can1);
if (cnt2 > nums.length / 3)
res.add(can2);
return res;
}
Majority Element II——LeetCode的更多相关文章
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- LeetCode(169)Majority Element and Majority Element II
一个数组里有一个数重复了n/2多次,找到 思路:既然这个数重复了一半以上的长度,那么排序后,必然占据了 a[n/2]这个位置. class Solution { public: int majorit ...
- 【LeetCode】229. 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
Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...
- Majority Element,Majority Element II
一:Majority Element Given an array of size n, find the majority element. The majority element is the ...
- Majority Element(169) && Majority Element II(229)
寻找多数元素这一问题主要运用了:Majority Vote Alogrithm(最大投票算法)1.Majority Element 1)description Given an array of si ...
- LeetCode169 Majority Element, LintCode47 Majority Number II, LeetCode229 Majority Element II, LintCode48 Majority Number III
LeetCode169. Majority Element Given an array of size n, find the majority element. The majority elem ...
- [LeetCode] 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 ...
随机推荐
- HTML select 操作
今天遇到一个问题,就是想设置select的默认选择项.但是试了很多方法都不行: <fieldset data-role="contractstatus"> <la ...
- ASP.NET MVC 第三回 Controller与View
这节我们让ASP.NET MVC真正的跑起来 一.新建Controller 首先我们自己新建一个新的Controller在Controllers上点右键,添加,Controller选项 之后出现一 ...
- Android开发手记(28) Handler和Looper
Android的消息处理有三个核心类:Looper,Handler和Message.其实还有一个Message Queue(消息队列),但是MQ被封装到Looper里面了,我们不会直接与MQ打交道.平 ...
- 安装Visual Studio 2010时提示"The location specified for the help content store is invalid or you do not have access to it".
运行注册表: (运行->输入"regedit").在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Help\v1.0中,删除"Loc ...
- 打造属于前端的Uri解析器
今天和大家一起讨论一下如何打造一个属于前端的url参数解析器.如果你是一个Web开发工程师,如果你了解过后端开发语言,譬如:PHP,Java等,那么你对下面的代码应该不会陌生: $kw = $_GET ...
- 第一次用Github desktop(mac)提交代码遇到的问题
1.新建代码仓库 2.生成密钥 ssh-keygen -C 'your@email.address' -t rsa 3.到根目录下的.ssh文件夹下找到id_rsa.pub文件,将里面的内容复制到下图 ...
- C# string转int
1,int转成string用toString 或者Convert.toString()如下 例如:int varInt = 1; string varString = Convert.ToString ...
- java_设计模式_工厂模式_Factory Pattern(2016-08-04)
工厂模式主要是为创建对象提供了接口.工厂模式按照<Java与模式>中的提法分为三类: (1)简单工厂(Simple Factory)模式,又称静态工厂方法模式(Static Factory ...
- Android三种菜单简介
Android的菜单分为三种类型:选项菜单(Option Menu).上下文菜单(Context Menu).子菜单(Sub Menu). 一.选项菜单 用户点击设备上的菜单按钮(Menu),触发事件 ...
- [翻译]jQuery十周年-John Resig
10th Anniversary of jQuery Today marks the 10th anniversary of the release of jQuery...[原文] 今天是jQuer ...