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 ...
随机推荐
- hdu 4223 Dynamic Programming?
Dynamic Programming? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- LAMP配置
参考资料: 1. http://www.cnblogs.com/zhuyp1015/p/3561470.html ↂ Apache服务器: 安装Apache: sudo apt-get install ...
- hdu2037-----------贪心, 活动安排问题
http://acm.hdu.edu.cn/showproblem.php?pid=2037(简单贪心-----活动安排) #include<iostream> #include<a ...
- XAF应用开发教程(一) 创建项目
XAF是DevExpress公司的快速开发框架,全称eXpress Application Framework,是企业信息系统的开发利器,快速开发效果显著,在.net框架中,笔者至今没有找到一款可以与 ...
- Nemerle Quick Guide
This is a quick guide covering nearly all of Nemerle's features. It should be especially useful to a ...
- maven入门
1.1.项目构建 Maven(翻译为"专家","内行")是跨平台的项目管理工具.主要服务于基于Java平台的项目构建,依赖管理和项目信息管理. 项目构建过程包括 ...
- 使用xml方式定义补间动画
在res下创建一个目录 anim目录 public class MainActivity extends Activity { private ImageView iv; @Override prot ...
- return
return作为返回关键字,有以下两种意义的返回格式: 1,返回把握与函数成果:停止函数执行,返回调用函数,并且把函数的值作为返回成果. turn只能退出当前函数,如果多个函数嵌套就不行了,要想整个退 ...
- [js] 非常好的面试题,学习了。。
原文链接:http://www.cnblogs.com/xxcanghai/p/5189353.html
- 修正ios h5上传图时的图片方向问题
.ios上传会在exif中带一个 Orientation的属性,这个属性在windows中不会生效,在ios浏览器中会生效,造成图片在windows资源管理器中与ios浏览器中方向不一致 为了用户 ...