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 ...
随机推荐
- Python 中的容器 collections
写在之前 我们都知道 Python 中内置了许多标准的数据结构,比如列表,元组,字典等.与此同时标准库还提供了一些额外的数据结构,我们可以基于它们创建所需的新数据结构. Python 附带了一个「容器 ...
- 基于Ubuntu搭建Linux路由器
开源,几乎代表了无所不能的意思,最近又因为它玩Hi了... 因业务发展,需要临时接入300MB的专线和千兆路由器,而公司现有的路由器却是百兆的,出于成本考虑,只能不想更换新的路由器,在网上查了一下可以 ...
- Ubuntu 进阶命令——长期不定时更新
有时候远程连接服务器忽然中断或者不小心关掉了终端界面,正在运行的命令或者程序就会被强制停止.这时候,我们可以借助一些命令来避免这种情况的发生. nohup 不挂断地运行命令 & 在后台运行命令 ...
- iOS-plist文件的写读
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"xiaoli" ofType:@"plist ...
- PokeCats开发者日志(六)
现在是PokeCats游戏开发的第九天的晚上,终于将这玩意提交到360移动开放平台进行审核了. 貌似很多平台都需要看这个著作权证明,得了,那我就话400块钱走一遍流程玩玩吧! 办理著作权还 ...
- 高级C代码的汇编分析
在windows上,常用的函数调用方式有: Pascal方式,WINAPI(_stdcall)方式 和C方式(_cdecl) _cdecl调用规则: 1,参数从右到左入堆栈 2,在函数返回后,调用者要 ...
- [剑指Offer] 9.变态跳台阶
题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. [思路1]每个台阶都有跳与不跳两种可能性(最后一个台阶除外),最后一个台阶必 ...
- P3539 [POI2012]ROZ-Fibonacci Representation
题目描述 The Fibonacci sequence is a sequence of integers, called Fibonacci numbers, defined as follows: ...
- [Leetcode] subsets ii 求数组所有的子集
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- BZOJ2120 数颜色 【带修改莫队】
2120: 数颜色 Time Limit: 6 Sec Memory Limit: 259 MB Submit: 6579 Solved: 2625 [Submit][Status][Discus ...