LeetCode 4 :Majority Element
problem:Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
problem analysis:
1.首先,需要统计数组元素出现的次数,包括不同的元素有几个?每一个不同的元素出现了几次?同时需要将不同的元素及出现的频率正确对应。
2.在第一步完成之后,可以寻找最大的频率数,然后找到majority element。
代码写的有点乱,有待优化。
class Solution
{
public:
int majorityElement(vector<int> &num)
{
vector<int> elemFre;
vector<int> elemCount(num.size(), );
int vecSize = num.size()/;
int tempInt = ;
int vecPos = ;
int noMatchNum = ;
int elemSize = ;
for(vector<int>::iterator iter=num.begin(); iter!=num.end(); ++iter)
{
if(iter == num.begin())
{
elemFre.push_back(*iter);
}
for(vector<int>::iterator iterFre=elemFre.begin(); iterFre!=elemFre.end(); ++iterFre)
{
if((*iter) == (*iterFre))
{
elemCount[noMatchNum] += ;
}
else
{
++noMatchNum;
continue;
}
}
elemSize = elemFre.size();
if(noMatchNum == elemSize)
{
elemFre.push_back(*iter);
elemCount[noMatchNum] += ;
}
noMatchNum = ;
}
vecPos = ;
for(vector<int>::iterator iter=elemCount.begin(); iter!=elemCount.end(); ++iter,++vecPos)
{
if((*iter) > vecSize)
{
tempInt = elemFre[vecPos];
}
else
{
continue;
}
}
return tempInt;
}
};
第一个两重for循环建立一个元素及其频率的对应表,elemFre存储的是num中不同的元素,elemCount存储的是elemFre里面元素出现的频率。
1.第一次循环,把第一个元素添加到elemFre,进入内层循环中。
2.内层循环,遍历elemFre中的所有元素,分两种情况:a,num中的元素在elemFre中找到了对应项,则对应的频率数组elemCount[noMatchNum]+1;否则noMatchNum+1.直到将elemFre中所有的元素都遍历完。
3.循环结束后,noMatchNum中应该存放的是与当前元素都不同的之前出现的元素数目,或者是当前元素在elemFre中的对应项前面的元素数目。
4.在上面的基础上判断noMatchNum == elemFre.size(),如果相等,则说明新到元素需要添加到elemFre中,并且对应的频率数+1,具体完成这一操作的是
if(noMatchNum == elemSize)
{
elemFre.push_back(*iter);
elemCount[noMatchNum] += 1;
}
noMatchNum = 0;
这样通过上面的操作完成的不同元素的提取,和对应频率数目的统计工作。
5.通过以上操作,在遍历一次elemCount,找到最大的频率数并返回对应的元素即可。
总计:逻辑思维不够,思维不严谨,变量的最终结果意义不明确,浮躁。
我这个方法肯定不是最好的,欢迎大家推荐优化的算法。
LeetCode 4 :Majority Element的更多相关文章
- [LeetCode] 169. Majority Element 多数元素
Given an array of size n, find the majority element. The majority element is the element that appear ...
- [LeetCode] 229. Majority Element II 多数元素 II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...
- LeetCode 169. Majority Element (众数)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- leetcode 169 Majority Element 冰山查询
Given an array of size n, find the majority element. The majority element is the element that appear ...
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- LeetCode 169. Majority Element - majority vote algorithm (Java)
1. 题目描述Description Link: https://leetcode.com/problems/majority-element/description/ Given an array ...
- 【leetcode】Majority Element
题目概述: Given an array of size n, find the majority element. The majority element is the element that ...
- ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Java for LeetCode 169 Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- [推荐]spring cloud 详解
http://blog.csdn.net/column/details/15197.html
- 机器学习实战一:kNN手写识别系统
实战一:kNN手写识别系统 本文将一步步地构造使用K-近邻分类器的手写识别系统.由于能力有限,这里构造的系统只能识别0-9.需要识别的数字已经使用图形处理软件,处理成具有相同的色彩和大小:32像素*3 ...
- redis-Windows下安装与操作
Redis windows下安装 1.安装 (1)windows把redisbin_x32安装包放在电脑任意的盘里 (2)通过cmd找到对应目录: D\redisbin_x32 (3)开始安装 D\ ...
- 无法注册DLL/OCX:regsvr32 failed with exit code 0x5
最近在服务器上装一个等值线控件,确报错: RegSvr32 failed with exit code 0x5 尝试解决方法1: 在cmd下 运行for %1 in (%windir%\syste ...
- 移动端webapp如何隐藏浏览器的导航栏
webapp如何隐藏浏览器的导航栏 在webapp开发中,手机浏览器的导航栏会让我们的页面看起来很怪异,这个时候我们就需要将导航栏给隐藏起来,隐藏的方法十分简单,只需要在head头中加入以下几行代码就 ...
- Windows下LATEX排版论文攻略—CTeX、JabRef使用心得
笔者刚刚接触到TEX排版,相关知识完全空白,用了两天时间学习并完成了一篇论文的完整排版. 期间遇到不少小问题,着实辛苦,分享至上,现将其解决办法总结归纳,共同学习. 一.工具介绍 TeX是一个很好排版 ...
- 判断python字典中key是否存在的两种方法
今天来说一下如何判断字典中是否存在某个key,一般有两种通用做法,下面为大家来分别讲解一下: 第一种方法:使用自带函数实现. 在python的字典的属性方法里面有一个has_key()方法,这个方法使 ...
- STL Allocator
从上面这个程序可以看出,我们这里手动使用了分配器,分配器有很多种类,有std::,还有非std::,也就是上面的__gnu_cxx下面的,我们在使用容器的时候不关心我们使用什么分配器,也不关心我们如何 ...
- JQuery事件对象的属性和方法
这是今天的总结,以后学习自己可以当参考书来读读.Event 对象代表事件的状态,比如事件在其中发生的元素.键盘按键的状态.鼠标的位置.鼠标按钮的状态.事件通常与函数结合使用,函数不会在事件发生前被执行 ...
- BZOJ4592 SHOI2015脑洞治疗仪(线段树)
考虑需要资瓷哪些操作:区间赋值为0:统计区间1的个数:将区间前k个0变为1:询问区间最长全0子串.于是线段树维护区间1的个数.0的个数.最长前缀后缀全0子串即可.稍微困难的是用一个log实现将区间前k ...