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. Cygwin解决Windows远程登录linux服务器

    下载地址http://www.cygwin.com/install.html 选择mirror.htnshost.com网站下载的比较快. 安装Cygwin(/X)需要选择的包: openssh(必选 ...

  2. 基础数据结构 之 队列(python实现)

    队也是编程开发中常见的一种数据结构.栈和队可用来模拟函数的递归过程.队的特点为先入先出,主要操作包括入队和出队.入队时需判断队是否已满,出队时需判断队是否为空.下面给出一个队的python实现的例子: ...

  3. URAL 1784 K - Rounders 找规律

    K - RoundersTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view. ...

  4. c# 自己制作一个简单的项目倒计时器

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. 发布站点到远程FTP根目录

    第一步:修改 phpcms/modules/admin/templates/site_add.tpl.php 找到 <legend><?php echo L('release_poi ...

  6. iOS开发——数据持久化&使用NSUserDefaults来进行本地数据存储

    使用NSUserDefaults来进行本地数据存储   NSUserDefaults适合存储轻量级的本地客户端数据,比如记住密码功能,要保存一个系统的用户名.密码.使用NSUserDefaults是首 ...

  7. Keeplived 配制图解

    http://blog.csdn.net/tantexian/article/details/50056229

  8. XCode中调整字体大小

    转自:http://www.cnblogs.com/mengshu-lbq/archive/2012/12/24/2830859.html 1)调出Xcode的preference应该是 Comman ...

  9. c#线程问题(2)

    表示要在新上下文中调用的方法.public delegate void ContextCallback(Object state) 参数 state 类型: System. Object一个对象,包含 ...

  10. extern的困惑

    摘自:http://blog.csdn.net/fxjtoday/article/details/6021845 如果想明白为什么需要extern, 需要从编译和链接讨论起, 现代编译器一般采用按文件 ...