【LeetCode】169 - 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.
You may assume that the array is non-empty and the majority element always exist in the array.
Solution 1: 使用map计数
class Solution {
public:
int majorityElement(vector<int>& nums) { //runtime:32ms
map<int, int> nums_count;
vector<int>::iterator iter=nums.begin();
while(iter!=nums.end()){
++nums_count[*iter];
iter++;
}
int n=nums.size();
map<int, int>::iterator ite=nums_count.begin();
//int max=ite->second;
while(ite!=nums_count.end()){
if(ite->second>n/){
return ite->first;
}
ite++;
}
}
};
Solution 2: 每找出两个不同的element就成对删除,最后可能剩两个元素或一个元素,必定都是所求
class Solution {
public:
int majorityElement(vector<int>& nums) { //runtime: 20ms
int count=;
int ret;
for(int i=;i<nums.size();i++){
if(count==){
ret=nums[i];
count++;
}else{
if(nums[i]==ret)
count++;
else
count--;
}
}
return ret;
}
};
扩展到⌊ n/k ⌋的情况,每k个不同的element进行成对删除
【LeetCode】169 - Majority Element的更多相关文章
- 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore ...
- 【LeetCode】229. Majority Element II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...
- 【一天一道LeetCode】#169. Majority Element
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【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❤python】169. Majority Element
#Method 1import math class Solution(object): def majorityElement(self, nums): numsDic={} ...
- 【刷题-LeetCode】229. Majority Element II
Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...
- 【原创】leetCodeOj --- Majority Element 解题报告(脍炙人口的找n个元素数组中最少重复n/2次的元素)
题目地址: https://oj.leetcode.com/problems/majority-element/ 题目内容: Given an array of size n, find the ma ...
- LeetCode OJ 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode Problem 169: Majority Element查找多数元素
描述:Given an array of size n, find the majority element. The majority element is the element that app ...
随机推荐
- MySQL 数据库性能优化之索引优化
接着上一篇 MySQL 数据库性能优化之表结构,这是 MySQL数据库性能优化专题 系列的第三篇文章:MySQL 数据库性能优化之索引优化 大家都知道索引对于数据访问的性能有非常关键的作用,都知道索引 ...
- Echarts - js
<script type="text/javascript"> var myChart; myChart = echarts.init(document.getElem ...
- ajax返回JSON时的处理方式
JSON中对象通过“{}”来标识,一个“{}”代表一个对象,如{“AreaId”:”123”},对象的值是键值对的形式(key:value). json_encode() 该函数主要用来将数组和对象, ...
- MVC列表页通过CheckBox进行批量选择删除
1.Html代码,将所有CheckBox包含于删除表单,并且所有列表项的CheckBox使用相同的Name,并且Value设置为数据项主键ID @using (Html.BeginForm(" ...
- 转:ViewPager+Fragment基本使用方法(附源码)
ViewPager+Fragment可以做出多页面滑动效果,让我们的应用程序界面操作起来更加灵活 对于ViewPager和Fragment组件还不熟悉的朋友,可以先看看相关的资料 首先在activit ...
- jquery总结(1)
jquery是一种js对象.里面封装了一些方法,但是jquery对象不能直接使用js方法,js对象不能直接使用jquery方法. jquery对象类似于js对象的集合,就是存在形式是以特殊数组的形式: ...
- BZOJ 1003 物流运输
最短路+dp. #include<iostream> #include<cstdio> #include<cstring> #include<algorith ...
- django - 修改 自增长id,起始值
常常你会遇到这样的情况,需要自增长的起始值是 0,再次从 0开始. 两个选择: 1. drop table_name; django重新建表. 2. ALTER TABLE table_name AU ...
- ubuntu下Rhythmbox播放器乱码问题解决方案
(注:本文部分内容转自互联网)<a href="http://riden001.com/wp-content/uploads/2014/11/45.jpg"><i ...
- linux下安装虚拟机qemu kqemu
一,为什么要装虚拟机,为什么选择qemu 我的系统里面有3个linux系统,这些系统都是独立的,有的时候,我想一台电脑,能更真实的模拟二台,这个时候我们就可以装个虚拟机.其实如果真的很有钱的话,可能考 ...