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. php error file_get_contents()

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  2. AlertView with password

    1. setAlertViewStyle:UIAlertViewStyleSecureTextInput UIAlertView *alertView = [[UIAlertView alloc] i ...

  3. 经常使用的android弹出对话框

    我们在平时做开发的时候,免不了会用到各种各样的对话框,相信有过其它平台开发经验的朋友都会知道,大部分的平台都仅仅提供了几个最简单的实现,假设我们想实现自己特定需求的对话框,大家可能首先会想到,通过继承 ...

  4. delphi HTML代码

    ################################属性 设置################################################字体设置{//-------- ...

  5. 【android】在eclipse中查看genymotion模拟器的sd卡文件夹

    假设用google自带模拟器或者真机调试时,sd卡文件夹是在/mnt/sdcard.这个相信大家都知道. 但是今天用genymotion调试时.发现根本打不开/mnt/sdcard这个文件夹,当时也没 ...

  6. IOS 日期选择

    传统方式 一般情况下弹出日期选择的场景是:用户点击UITextField弹出日期选择,关键代码如下: 点击UITextField弹出日期选择 1 2 3 UITextField *textField; ...

  7. 获取设备上全部系统app信息

    在获取android设备的全部程序信息一文中介绍了获取手机上全部app信息的方法,以下介绍过滤掉系统app的方法: MainActivity: package com.home.getsysapp; ...

  8. 如何本地化 Windows Phone 应用标题

    如何本地化 Windows Phone 应用标题 http://msdn.microsoft.com/zh-cn/library/windowsphone/develop/ff967550(v=vs. ...

  9. 嵌入式系统 Boot Loader 技术内幕

    转载:http://www.ibm.com/developerworks/cn/linux/l-btloader/index.html   1. 引言 在专用的嵌入式板子运行 GNU/Linux 系统 ...

  10. 小白日记28:kali渗透测试之Web渗透-扫描工具-Nikto

    扫描工具-Nikto #WEB渗透 靶机:metasploitable 靶场:DVWA[默认账号/密码:admin/password] #新手先将DVWA的安全性,调到最低,可容易发现漏洞 侦察[减少 ...