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 element is the element that appears more than ⌊ n/2 ⌋ times. (Easy)
You may assume that the array is non-empty and the majority element always exist in the array.
分析:
抵消的思想,维护一个result和count,与result相同则count++,否则count--,到0时更新result的值,由于主元素个数较多,所有最后一定能留下。
代码:
class Solution {
public:
int majorityElement(vector<int>& nums) {
int result = ;
int count = ;
for (int i = ; i < nums.size(); ++i) {
if (nums[i] == result && count != ) {
count++;
}
else if (count == ) {
result = nums[i];
count = ;
}
else {
count--;
}
}
return result;
}
};
LintCode47. Majority NumberII
Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array. (Medium)
Find it.
Notice : There is only one majority number in the array.
分析:
同majority number1的思路相似,维护两个result和count,相同则对相应count操作,不同则均减一;
注意最后剩下两个元素,并不一定count值大的就一定是出现次数多的(可能另一个参与抵消过多),所以需要重新遍历一遍,对这个两个数比较出现次数大小。
代码:
class Solution {
public:
/**
* @param nums: A list of integers
* @return: The majority number occurs more than 1/3.
*/
int majorityNumber(vector<int> nums) {
// write your code here
int candidate1 = , candidate2 = ;
int count1 = , count2 = ;
for (int i = ; i < nums.size(); ++i) {
if (nums[i] == candidate1 && count1 != ) {
count1++;
}
else if (nums[i] == candidate2 && count2 != ) {
count2++;
}
else if (count1 == ) {
candidate1 = nums[i];
count1 = ;
}
else if (count2 == ) {
candidate2 = nums[i];
count2 = ;
}
else {
count1--;
count2--;
}
}
count1 = ;
count2 = ;
for (int i = ; i < nums.size(); ++i) {
if (nums[i] == candidate1) {
count1++;
}
else if (nums[i] == candidate2) {
count2++;
}
}
return count1 > count2 ? candidate1 : candidate2;
}
};
LeetCode229. 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.(Medium)
分析:
这个与lintcode中的majority number2基本相似,只是要求找到所有的大于n / 3次的元素(至多也就是两个);
所以最后一步从比较两个canditate的count大小,变成将这两个count与 size() / 3比较。
代码:
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
int candidate1 = , candidate2 = ;
int count1 = , count2 = ;
for (int i = ; i < nums.size(); ++i) {
if (nums[i] == candidate1 && count1 != ) {
count1++;
}
else if (nums[i] == candidate2 && count2 != ) {
count2++;
}
else if (count1 == ) {
candidate1 = nums[i];
count1 = ;
}
else if (count2 == ) {
candidate2 = nums[i];
count2 = ;
}
else {
count1--;
count2--;
}
}
count1 = ;
count2 = ;
for (int i = ; i < nums.size(); ++i) {
if (nums[i] == candidate1) {
count1++;
}
else if (nums[i] == candidate2) {
count2++;
}
}
vector<int> result;
if (count1 > nums.size() / ) {
result.push_back(candidate1);
}
if (count2 > nums.size() / ) {
result.push_back(candidate2);
}
return result;
}
};
LintCode48. Majority Number III
Given an array of integers and a number k, the majority number is the number that occurs more than 1/k of the size of the array. (Medium)
Find it.
Notice:There is only one majority number in the array.
分析:
从前一题的1/3变为1/k,道理还是一样,不过这次需要用一个hashmap来维护出现的次数,注意unordered_map插入删除相关操作的写法即可。
尤其hashmap元素个数等于k需要删除的时候,需要维护一个vector存key,如果用iterator边走边删除可能出现位置变化。
代码:
class Solution {
public:
/**
* @param nums: A list of integers
* @param k: As described
* @return: The majority number
*/
int majorityNumber(vector<int> nums, int k) {
// write your code here
unordered_map<int, int> hash;
for (int i = ; i < nums.size(); ++i) {
if (hash.size() < k) {
hash[nums[i]]++;
}
else {
vector<int> eraseVec;
for (auto itr = hash.begin(); itr != hash.end(); ++itr) {
(itr -> second)--;
if (itr -> second == ) {
eraseVec.push_back(itr -> first);
}
}
for (int i = ; i < eraseVec.size(); ++i) {
hash.erase(eraseVec[i]);
}
hash[nums[i]]++;
}
}
for (auto& n : hash) {
n.second = ;
}
for (int i = ; i < nums.size(); ++i) {
if (hash.find(nums[i]) != hash.end()) {
hash[nums[i]]++;
if (hash[nums[i]] > nums.size() / k) {
return nums[i];
}
}
}
return -;
}
};
LeetCode169 Majority Element, LintCode47 Majority Number II, LeetCode229 Majority Element II, LintCode48 Majority Number III的更多相关文章
- [Swift]LeetCode503. 下一个更大元素 II | Next Greater Element II
Given a circular array (the next element of the last element is the first element of the array), pri ...
- [LeetCode] 305. Number of Islands II 岛屿的数量 II
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- lintcode 中等题:N Queens II N皇后问题 II
题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...
- HDU 3567 Eight II(八数码 II)
HDU 3567 Eight II(八数码 II) /65536 K (Java/Others) Problem Description - 题目描述 Eight-puzzle, which is ...
- DE1-SOC开发板上搭建NIOS II处理器运行UCOS II
DE1-SOC开发板上搭建NIOS II处理器运行UCOS II 今天在DE1-SOC的开发板上搭建NIOS II软核运行了UCOS II,整个开发过程比较繁琐,稍微有一步做的不对,就会导致整个过 ...
- 杨辉三角形II(Pascal's Triangle II)
杨辉三角形II(Pascal's Triangle II) 问题 给出一个索引k,返回杨辉三角形的第k行. 例如,给出k = 3,返回[1, 3, 3, 1] 注意: 你可以优化你的算法使之只使用O( ...
- [Swift]LeetCode407. 接雨水 II | Trapping Rain Water II
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- 【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】
[059-Spiral Matrix II(螺旋矩阵II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an integer n, generate a ...
- Leetcode之回溯法专题-212. 单词搜索 II(Word Search II)
Leetcode之回溯法专题-212. 单词搜索 II(Word Search II) 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单 ...
随机推荐
- day19 作业
今日作业 1.什么是对象?什么是类? 对象:特征和技能的结合体 类:一系列对象 相同的特征和技能的结合体 2.绑定方法的有什么特点 对象调用类内部的函数 称之为绑定方法,特点: 不同的对象调用该绑定方 ...
- 判断django中的orm为空
result= Booking.objects.filter() #方法一 .exists() if result.exists(): print "QuerySet has Data&qu ...
- bzoj4788: [CERC2016]Bipartite Blanket
2019.1.9交流题,现在看还是不会,,, 如果只有一边,那么Hall定理即可. 两边?分别满足Hall定理,就是合法的! 证明(构造方案): 左集合先任意形成一个合法匹配,单点增量加入右集合和与右 ...
- 19-10-24-H
H H H H H H ZJ一下: T1只会暴力,测试点分治. (表示作者的部分分并没有给够,暴力加部分表按测试点分类可以得60吧……) T2先直接手玩第一个子任务. 然后就$Find$了一个神奇的( ...
- autoreleasing的用法介绍:
在c/c++,objective-c内存管理中有一条是:谁分配谁释放. __autoreleasing则可以使对像延迟释放.比如你想传一个未初始化地对像引用到一个方法当中,在此方法中实始化此对像,那么 ...
- MyBatis配置文件(一)――properties属性
MyBatis配置文件中有很多配置项,这些配置项分别代表什么,有什么作用,需要理一下了.先通过下面这个例子来看都有哪些配置项 <?xml version="1.0" enco ...
- 数据表中记录明明有,session.get(类.class, id);返回null
出现null的处理思路首先检查数据库中是否真的有这个记录 确实存在的,用接口查一下最大值,也是存在的,数据库连接正常 写sql也可以查得到 然而诡异的事情出现了 难道是一直在用的dao代码出了问题? ...
- ms12-020复现-xp蓝屏
漏洞名:MS12-020(全称:Microsoft windows远程桌面协议RDP远程代码执行漏洞) 介绍:RDP协议是一个多通道的协议,让用户连上提供微软终端机服务的电脑. windows在处理某 ...
- ajax无刷新上传文件
网页上传文件最简单的方式就是通过表单上传了,但是在提交表单的时候会导致网页刷新,但有的时候我们不想网页刷新,有什么办法呢,我们可以使用ajax上传文件来做到这一点.只有ajax还不行,还需要JavaS ...
- css3之弹性盒模型(Flex Box)
CSS3 弹性盒子(Flex Box) 弹性盒子是 CSS3 的一种新的布局模式. CSS3 弹性盒( Flexible Box 或 flexbox),是一种当页面需要适应不同的屏幕大小以及设备类型时 ...