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. Kotlin怎样使用Android的Dagger2

    作者:Antonio Leiva 时间:Apr 11, 2017 原文链接:https://antonioleiva.com/dagger-android-kotlin/ 在Android上,创建去耦 ...

  2. POI HSS 合并重复的列

    import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; i ...

  3. MySQL 5.6查看数据库的大小

    1. use information_schema; 2. select concat(round(sum(data_length/1024/1024),2),'MB') as data from t ...

  4. Git&GitHub&GitBook

    一.定义 Git(分布式版本控制系统) GitHub gitHub是一个面向开源及私有软件项目的托管平台,因为只支持git 作为唯一的版本库格式进行托管,故名gitHub. gitHub于2008年4 ...

  5. c#和html方法互调

    具体见连接:https://www.cnblogs.com/zeroLove/p/3912460.html

  6. C++结构体排序

    在C++中,对结构体的排序方式比C语言丰富的多.在C语言中,我们主要是通过qsort进行排序操作(抛开手写排序算法不说). 在C++<algorithm>中,有一个十分强大的排序函数sor ...

  7. 软工实践Beta冲刺(3/7)

    队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 1.界面的修改与完善 展示GitHub当日代码/文档签入记 ...

  8. Android Studio的初体验

    在机缘巧合之下遇到了安卓开发,接触了Android Studio开始了漫长的改bug的道路,以下为简易版心酸历程 首先我需要成功安装Android Studio,由于我过于叛逆以及为了避免出错于是从一 ...

  9. Log4Net讲解

    声明:本文内容主要译自Nauman Leghari的Using log4net,亦加入了个人的一点心得(节3.1.4). 1           简介 1.1          Log4net的优点: ...

  10. js获取上传文件内容

    js 获取上传文件的字节数及内容 <div> 上传文件 : <input type="file" name = "file" id = &qu ...