【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 ...
随机推荐
- 路径名称和struts.xml配置不一致导致struts2报404
struts.xml中写的是<result name="...">authorityInterceptor/xxx.jsp</result> 但是实际的文件 ...
- USACO Section 2.2: Subset Sums
dp题,一碰到dp我基本就是跪,搜了网上的答案分两种,一维和二维. 先讲二维,sum[i][j]表示前i个数的subset里差值为j的分法数量.当加入数字i时,有两种选择,某一个set和另外一个set ...
- 《Java编程那点事儿》读书笔记(三)——static,this,抽象类,接口和包
1. static 1)静态变量:Java虚拟机为静态变量开辟单独的存储空间,所以所有的对象内部的静态变量在内存中都指向同一个地址,那么不管哪个对象改变这个成员变量,所有对象中该成员变量的值都发生变化 ...
- framework-Binder
init进程fork servicemanager进程用来提供(server)注册service和(client)检索service功能.servicemanager维护了一个service列表,cl ...
- R之批处理
在linux下如何编写脚本调用R语言写的程序呢? R语言进行批处理有2种方式: R CMD BATCH --options scriptfile outputfile Rscript --option ...
- 1.Cadence16.5的安装教程[原创]
http://jingyan.baidu.com/article/6d704a1319107a28db51cac9.html
- HibernateTools实现pojo类 数据库schma mapping映射的相互转换
核心 利用HibernateTools,从POJO类,Mapping映射文件,数据库表有其中的一项,就能生成其他两项. 概述 在使用Hibernate开发系统持久层时,按照一般开发流程 1.分析业务 ...
- httpClient.execute之后一直等待
可能的原因就是之前执行过一次execute,但是没有释放资源. hrp = httpClient.execute(req); //这句释放资源 hrp.getEntity().consumeConte ...
- JS 去除字符串中的空格
1. 去掉字符串前后所有空格: 代码如下: function Trim(str) { return str.replace(/(^\s*)|(\s*$)/g, ""); } 说明: ...
- Spring MVC的UrlBasedViewResolver和InternalResourceViewResolver
Spring MVC使用ViewResolver来根据controller中返回的view名关联到具体的View对象.使用View对象来渲染返回值以生成最终的视图,如html,json或pdf等. S ...