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. (六)ICMP、ping

    ping命令是ICMP协议 127.0.0.1 ifconfig -all

  2. Windows 数据类型

    类型 定义 描述 ATOM typedef WORD ATOM; 原子(原子表中的一个字符串的参考) BOOL typedef int BOOL; 布尔变量 BOOLEAN typedef BYTE ...

  3. 常用sql语句总结

    一.连接数据库 mysql -h127.0.0.1(连接地址) -P3303(端口号) -uroot(用户名) -p123456(密码) 二.常用语句 show datebases; 查看数据库 cr ...

  4. VIM 代码折叠

    VIM 代码折叠 VIM代码折叠方式可以用"foldmethod"选项来设置,如: set foldmethod=indent 有6种方式来折叠代码 1. manual //手工定 ...

  5. (33)odoo中产品价格字段

    打开product.template 和 product.product 模型发现有很多关于价格描述的字段 product.template:        price        list_pri ...

  6. 制作linux内核安装包

    实验基于Centos 6.2 升级linux内核 直接在一个有编译环境的设备上,编译升级内核很简单. make menuconfig 或者 拷贝现有系统的.config文件 修改.config文件  ...

  7. maven入门探讨

    java项目最恶心的一点莫过于需要使用大量的jar.每次引用jar的时候都要自己手动去各地寻找,然后导入到项目的指定文件夹当中最后还要添加Path.这无疑是一项工作量巨大的工作,同时如果控制不当就会提 ...

  8. Java 概述

    一 Java 程序的种类 1)Java 小应用程序(Java Applet) — 在Web浏览器中运行(内嵌Java虚拟机) —特定标记 <APPLET CODE="HelloWorl ...

  9. MyBatis关联查询 (association) 时遇到的某些问题/mybatis映射

    先说下问题产生的背景: 最近在做一个用到MyBatis的项目,其中有个业务涉及到关联查询,我是将两个查询分开来写的,即嵌套查询,个人感觉这样更方便重用: 关联的查询使用到了动态sql,在执行查询时就出 ...

  10. 1.JavaScript面试

    1.1. 简要描述 JavaScript 的数据类型?   参考答案:   JavaScript 的数据类型可以分为原始类型和对象类型. 原始类型包括 string.number 和 boolean  ...