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的更多相关文章

  1. [LeetCode] 169. Majority Element 多数元素

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  2. [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 ...

  3. LeetCode 169. Majority Element (众数)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  4. leetcode 169 Majority Element 冰山查询

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  5. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  6. LeetCode 169. Majority Element - majority vote algorithm (Java)

    1. 题目描述Description Link: https://leetcode.com/problems/majority-element/description/ Given an array ...

  7. 【leetcode】Majority Element

    题目概述: Given an array of size n, find the majority element. The majority element is the element that ...

  8. ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  9. LeetCode 169. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  10. Java for LeetCode 169 Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

随机推荐

  1. 「日常训练」 Soldier and Cards (CFR304D2C)

    题意 (Codeforces 546C) 按照指定的规则打牌,问谁胜或无穷尽. 分析 又是一条模拟,用set+queue(这里手写了)处理即可.注意到两种局势"1 234"和&qu ...

  2. No resources found. Error from server (NotAcceptable): unknown (get pods)

    问题:正确安装kubectl后,可查询到当前使用集群服务,也可切换不同的集群,但无法获取当前运行的pods的信息与所有的service,具体表现为 $ kubectl get po -nwx No r ...

  3. 剑指offer-旋转数组的最小数字06

    题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋 ...

  4. 软件工程项目组Z.XML会议记录 2013/10/22

    软件工程项目组Z.XML会议记录 [例会时间]2013年10月22日星期二21:00-22:30 [例会形式]小组讨论 [例会地点]三号公寓楼会客厅 [例会主持]李孟 [会议记录]周敏轩 会议整体流程 ...

  5. android桌面悬浮窗仿QQ手机管家加速效果

    主要还是用到了WindowManager对桌面悬浮进行管理. 需要一个火箭的悬浮窗 一个发射台悬浮窗  ,判断火箭是否放到了发射台,如果放上了,则使用AsyTask 慢慢将火箭的图片往上移.结束后., ...

  6. C#编译和运行原理

    关于编译与内存的关系,以及执行时内存的划分 1.所谓在编译期间分配空间指的是静态分配空间(相对于用new动态申请空间),如全局变量或静态变量(包括一些复杂类型的 常量),它们所需要的空间大小可以明确计 ...

  7. AGC016C +/- Rectangle(构造)

    题目大意:给定H,W,h,w四个数,求是否满足矩阵的全部数之和和正数,h行w列之和为负数 如果h和w恰好是H,W的约数,则肯定不存在 否则肯定存在 只需要把h,w内每个元素填的足够大,然后小矩形的最后 ...

  8. [洛谷P2711]小行星

    题目大意:有$n$颗行星,每颗行星的位置是$(x,y,z)$.每次可以消除一个面(即$x,y$或$z$坐标相等)的行星,求消除这些行星的最少次数. 题解:最小割,对于每一颗小行星,从 x 面的出点向  ...

  9. BZOJ4785 [Zjoi2017]树状数组 【二维线段树 + 标记永久化】

    题目链接 BZOJ4785 题解 肝了一个下午QAQ没写过二维线段树还是很难受 首先题目中的树状数组实际维护的是后缀和,这一点凭分析或经验或手模观察可以得出 在\(\mod 2\)意义下,我们实际求出 ...

  10. 获取本地ip地址 C#

    与ipconfig获取的所有信息一致的方法: private void GetIp() { System.Diagnostics.Process cmdp= new System.Diagnostic ...