229. Majority Element II -- 找出数组中出现次数超过 ⌊ n/3 ⌋ 次的数
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.
class Solution {
public:
//O(n) Space compexity
vector<int> majorityElement01(vector<int>& nums) {
vector<int> result;
unordered_map<int, int> counts;
int n = nums.size();
for(auto item : nums){
counts[item]++;
if (counts[item] > n/ ){
result.push_back(item);
counts[item] = -n; // Tricky: make sure the item only can be put into result once.
}
}
return result;
}
//We know, there could be at most two numbers can be more than 1/3
//so, same as Majority Element I problem, we can have two counters.
vector<int> majorityElement02(vector<int>& nums) {
if(nums.size()<=) return nums;
//the same algorithm as Majority Element I problem
int majority1=, majority2=, cnt1=, cnt2=;
for(auto item: nums) {
if (cnt1 == && majority2 != item ) {
majority1 = item;
cnt1 = ;
} else if (majority1 == item) {
cnt1++;
} else if (cnt2 == ) {
majority2 = item;
cnt2 = ;
} else if (majority2 == item) {
cnt2++;
} else {
cnt1--;
cnt2--;
}
}
//re-check it again, in case there has less than two numbers of majority
cnt1 = cnt2 = ;
for (auto item : nums) {
if (majority1 == item) cnt1++;
else if (majority2 == item) cnt2++;
}
vector<int> result;
if (cnt1 > nums.size()/) result.push_back(majority1);
if (cnt2 > nums.size()/) result.push_back(majority2);
return result;
}
229. Majority Element II -- 找出数组中出现次数超过 ⌊ n/3 ⌋ 次的数的更多相关文章
- 如何在O(n)的时间复杂度内找出数组中出现次数超过了一半的数
方法一:每次取出两个不同的数,剩下的数字中重复出现次数超过一半的数字肯定,将规模缩小化.如果每次删除两个不同的数,这里当然不是真的把它们踢出数组,而是对于候选数来说,出现次数减一,对于其他数来说,循环 ...
- 剑指Offer:找出数组中出现次数超过一半的元素
题目:找出数组中出现次数超过一半的元素 解法:每次删除数组中两个不同的元素,删除后,要查找的那个元素的个数仍然超过删除后的元素总数的一半 #include <stdio.h> int ha ...
- 找出数组中出现次数超过一半的数,现在有一个数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数
找出数组中出现次数超过一半的数,现在有一个数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数 #include<iostream>using namespace s ...
- <C#>找出数组中重复次数最多的数值
给定一个int数组,里面存在重复的数值,如何找到重复次数最多的数值呢? 这是在某社区上有人提出的问题,我想到的解决方法是分组. 1.先对数组中的所有元素进行分组,那么,重复的数值肯定会被放到一组中: ...
- Java实现找出数组中重复次数最多的元素以及个数
/**数组中元素重复最多的数 * @param array * @author shaobn * @param array */ public static void getMethod_4(int[ ...
- 【LeetCode】229. Majority Element II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- 1. 找出数组中的单身狗OddOccurrencesInArray Find value that occurs in odd number of elements.
找出数组中的单身狗: 1. OddOccurrencesInArray Find value that occurs in odd number of elements. A non-empty ze ...
- [LeetCode] Find All Numbers Disappeared in an Array 找出数组中所有消失的数字
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...
随机推荐
- FreeSWITCH第三方库(视频)的简单介绍(二)
FreeSWITCH使用了大量的第三方库,本文档主要介绍视频相关库的信息: 音频相关库的信息介绍参考:http://www.cnblogs.com/yoyotl/p/5486753.html 其他相关 ...
- [Java解惑]类
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- sqlserver2008创建数据库 报 Cannot read property is filestream 此属性不可用于sql server 7.0 解决
在创建数据库的时候,报整个错误 Cannot read property is filestream 此属性不可用于sql server 7.0 按照网上的方法 (http://blog.csdn. ...
- android——学习:网格布局——GridLayout
Android一开始就提供了几种布局控件,如线性布局LinearLayout.相对布局RelativeLayout和表格布局TableLayout等,但在很多情况下,这些布局控件是不能满足要求的,因此 ...
- 寻找Linux单机负载瓶颈
寻找Linux单机负载瓶颈 服务器性能上不去,是哪里出了问题?IO还是CPU?只有找到瓶颈点,才能对症下药: 如何寻找Linux单机负载瓶颈,遵循的原则是不要推测,我们要通过测量的数据说话: 负载分两 ...
- Deep Learning: Activation Function
Sigmoid Function ReLU Function Tanh Function
- hdu 4163 Stock Prices 水
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #de ...
- iOS - Swift NSPoint 位置
前言 结构体,这个结构体用来表示事物的一个坐标点. public typealias NSPoint = CGPoint public struct CGPoint { public var x: C ...
- 流媒体基础实践之——Nginx-RTMP-module 指令详解
转载网址:http://blog.csdn.net/aoshilang2249/article/details/51483814
- struts2 if标签示例
下面总结一下struts2 中if标签的使用 (1)判断字符串是否为空 <s:if test="user.username==null or user.username==''&quo ...