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. PHP的array_merge()合并数组

    ,4];print_r(array_merge($arr1,$arr2));返回结果:Array(    [a] => 3    [b] => 2    [0] => 4)1注释:当 ...

  2. Qt Qml 汽车仪表

    上一个原文连接http://blog.csdn.net/z609932088/article/details/53946245 参考资料连接:链接: https://pan.baidu.com/s/1 ...

  3. ADVICE FOR SHORT-TERM MACHINE LEARNING RESEARCH PROJECTS(短期机器学习研究的建议)

    – Tim Rocktäschel, Jakob Foerster and Greg Farquhar, 29/08/2018 Every year we get contacted by stude ...

  4. LeetCode 24——两两交换链表中的节点

    1. 题目 2. 解答 新建一个哨兵结点作为头结点,然后每次交换相邻两个结点.并依次将它们连接到新链表中去,再将原链表中后面的结点也串到新链表后面.直至到达链尾或者剩余一个节点,则此时返回新链表的头结 ...

  5. LeetCode 876——链表的中间结点

    1. 题目 给定一个带有头结点 head 的非空单链表,返回链表的中间结点. 如果有两个中间结点,则返回第二个中间结点. 示例 1: 输入:[1,2,3,4,5] 输出:此列表中的结点 3 (序列化形 ...

  6. 安装一台Linux练习机前的考虑——主机规划与磁盘分区

    要安装一台Linux主机并不是那么简单的事,我们必须要针对distributions的特性.服务器软件的能力.未来的升级需求.硬件扩充性需求等来考虑,对于磁盘分区.文件系统.Linux操作较频繁的目录 ...

  7. PhpStorm 配置IDE

    IDE => Xdebug => Apache(XAMPP) => Firefox + easist Xdebug 1>XAMPP停止apache服务;2>在安装目录下找 ...

  8. (转) linux I/O优化 磁盘读写参数设置

    关于页面缓存的信息,可以用cat /proc/meminfo 看到.其中的Cached 指用于pagecache的内存大小(diskcache-SwapCache).随着写入缓存页,Dirty 的值会 ...

  9. perf原理再看

    vim ./arch/x86/kernel/hw_breakpoint.c perf如何控制采样的频率 perf采样不同的事件,得到的不是一样 cycles: 向PMU中增加不同的函数,增加不同 使用 ...

  10. Intellij IDEA将工程打包成jar包并执行

    打开File -> Project Structure -> Artifacts 点击“+”,选择“Jar”,选择Empty或From modules with dependencies, ...