Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

思路.1

排序,选择第n/2个数,调用STL的sort,所以时间复杂度是O(nlogn)

class Solution {
public:
int majorityElement(vector<int>& nums) {
sort(nums.begin(),nuns.end());
return nums[nums.size()/2];
}
};

  

思路2

设置一个计数,当count为0时设置majority的值,如果下一个值与majority相同则count+1,如果不同,则-1,当count==0时,对majority重新赋值,因为majority的个数肯定大于n/2所以最后>0的count的肯定是majority,这样,就只需要遍历一遍就可以求出majority,时间复杂度为O(n)

class Solution {
public:
int majorityElement(vector<int>& nums) {
int Majority = nums[0];
int count = 1;
for( int i = 1; i < nums.size(); i++ ){
if( count == 0 ){
count++;
Majority = nums[i];
}
else if( Majority == nums[i] ){
count++;
}
else if ( Majority != nums[i] ){
count--;
}
}
return Majority;
}
};

  

LeetCode【169. Majority Element】的更多相关文章

  1. LeetCode OJ 169. Majority Element

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

  2. LeetCode Problem 169: Majority Element查找多数元素

    描述:Given an array of size n, find the majority element. The majority element is the element that app ...

  3. leetcode 【 Find Peak Element 】python 实现

    题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...

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

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

  5. 23. leetcode 169. Majority Element

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  6. LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II

    169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...

  7. Leetcode#169. Majority Element(求众数)

    题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...

  8. 169. Majority Element - LeetCode

    Question 169. Majority Element Solution 思路:构造一个map存储每个数字出现的次数,然后遍历map返回出现次数大于数组一半的数字. 还有一种思路是:对这个数组排 ...

  9. Week1 - 169.Majority Element

    这周刚开始讲了一点Divide-and-Conquer的算法,于是这周的作业就选择在LeetCode上找分治法相关的题目来做. 169.Majority Element Given an array ...

随机推荐

  1. LTE Module User Documentation(翻译7)——无线环境地图(REM)、AMC 模型 和 CQI 计算

    LTE用户文档 (如有不当的地方,欢迎指正!) 12 Radio Environment Maps   通过使用类 RadioEnvironmentMapHelper  是可能输出文件 Radio E ...

  2. pytessact 出现Error2错误

    x pytessact安装后不起作用 一.没有安装tessact-ocr 点击下载,安装 http://jaist.dl.sourceforge.net/project/tesseract-ocr-a ...

  3. ERROR 1018 (HY000): Can't read dir of './test/' (errno: 13)

    不能查看mysql中数据库的表. 一.查看 mysql> desc test; ERROR 1046 (3D000): No database selected mysql> use te ...

  4. mac 安装redis

    一.下载 官网http://redis.io/ (搞不懂为啥被墙) 二.安装 将下载的tar.gz文件复制到 /usr/local 文件夹下 解压 sudo tar -zxvf redis3.1.6. ...

  5. 了解less跟sass

    less官网:http://www.bootcss.com/p/lesscss/ LESS编写技巧分享:http://www.xuanfengge.com/less-writing-skills-sh ...

  6. nginx配置文件结构

    nginx配置文件结构   全局参数 配置系统全局参数如:worker_processes 工作子进程数量.error_log 错误日志路径.pid 进程IDEvent一般是配置nginx工作模式及连 ...

  7. FACADE

    1 意图:为子系统中的一组接口提供一个一直的界面,Facade模式定义了一个高层接口.这个接口使得这一子系统更加容易使用, 2 动机,便于不需要更多功能的人通过Facade 简化使用 3 适用性: . ...

  8. HTML5 十大新特性(一)——语义标签

    说语义标签前先来理解下什么叫语义化,当下html是靠div+css来铸造页面的整体框架和结构的,通篇大量的div可读性极低,因此诞生了这些特殊的标签,简单地说就是见名知义,使页面更清晰,方便维护和开发 ...

  9. Windows下配置OpenGL环境

    这里编译工具为VS2012. 首先OpenGL的官网如下链接(英文) http://www.opengl.org http://www.opengl.org/resources/libraries/g ...

  10. jQuery实现加入购物车飞入动画效果

    <script src="jquery.js"></script> <script src="jquery.fly.min.js" ...