Total Accepted: 95925 Total Submissions: 239241 Difficulty: Easy

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. O(n*n):判断每个元素是否是majority

  2. O(n): 利用HashTable存储每个元素的个数,空间复杂度高

  3. O(nlogn):对数组排序,majority肯定在n/2位置处。

  4. O(nlogn):分而治之,分成两分A和B,如果A和B的majority相等,则其为整个数组的majority;否则,分别扫描计算A,B的majority元素个数(O(2n)),所以时间复杂度T(n) = T(n/2) + 2n = O(nlogn)。

  5. O(n):两两删除数组中两个不同的元素,最后剩下的就是多的元素。代码如下:

     int majorityElement(vector<int>& nums) {
    if (nums.empty())
    return -1; int candidate, count = 0; for (int i = 0; i < nums.size(); ++i) {
    if (count == 0) {
    candidate = nums[i];
    ++count;
    }
    else {
    if (candidate == nums[i])
    ++count;
    else
    --count;
    }
    } return candidate;
    }

169. Majority Element My Submissions Question的更多相关文章

  1. 169. Majority Element - LeetCode

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

  2. 169. Majority Element(C++)

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

  3. 23. leetcode 169. Majority Element

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

  4. 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 ...

  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(求众数)

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

  7. Week1 - 169.Majority Element

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

  8. 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore ...

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

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

随机推荐

  1. Python3.5.2官方文档学习备忘录

    网址:https://docs.python.org/3/ 虽然学习官方文档有些耗时,不过看最原版的还是感觉好一点,原汁原味没有曲解没有省略. 从命令行向Python传递参数,运行:python - ...

  2. Webservice服务中如何保持Session

    问题一:webservice服务中如果保持Session 调用Session 对于Web Service,每个方法的调用都会启动一个Session,可以用下面的方法来使多个调用在同一个Session里 ...

  3. RealSense 3D实感体验:前景广阔目前应用少

    腾讯数码讯(周硕)在去年的IDF大会上,英特尔着重展示了其全新的RealSense 3D实感技术,而厚度仅6mm堪称史上最薄平板的戴尔Venue 8 7000也成为首个搭载RealSense技术的产品 ...

  4. Js Pattern - Self Define Function

    This pattern is useful when your function has some initial preparatory work to do andit needs to do ...

  5. C++11新特性,利用std::chrono精简传统获取系统时间的方法

    一.传统的获取系统时间的方法 传统的C++获取时间的方法须要分平台来定义. 相信百度代码也不少. 我自己写了下,例如以下. const std::string getCurrentSystemTime ...

  6. debian分区方案(就这个看着靠谱点)转

    debian分区方案(就这个看着靠谱点)转 桌面系统/tmp 1G (仅用作临时文件) ext3/ext4/home Max (用户目录数据) ext3/ext4/usr 20G (软件) ext3/ ...

  7. graylog2 架构--转载

    原文地址:http://docs.graylog.org/en/latest/pages/architecture.html Architectural considerations There ar ...

  8. 非托管C++通过C++/CLI包装调用C# DLL

    项目中要给其它客户程序提供DLL做为接口,该项目是在.Net4.0平台下开发.终所周知.Net的各个版本之间存在着兼容性的问题,但是为了使用高版本运行平台的新特性,又不得不兼顾其它低版本平台客户程序的 ...

  9. Lastest Version Carprog Full V7.28 update and EEPROM reading

    Carprog Full has recently launched the newest V7.28 (with all software activated and all 21items Ada ...

  10. poj3080解题报告(暴力、最大公共子串)

    POJ 3080,题目链接http://poj.org/problem?id=3080 题意: 就是求m个长度为60的字符串的最长连续公共子串,2<=m<=10 规定: 1.最长公共串长度 ...