169. Majority Element My Submissions Question
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.
O(n*n):判断每个元素是否是majority
O(n): 利用HashTable存储每个元素的个数,空间复杂度高
O(nlogn):对数组排序,majority肯定在n/2位置处。
O(nlogn):分而治之,分成两分A和B,如果A和B的majority相等,则其为整个数组的majority;否则,分别扫描计算A,B的majority元素个数(O(2n)),所以时间复杂度T(n) = T(n/2) + 2n = O(nlogn)。
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的更多相关文章
- 169. Majority Element - LeetCode
Question 169. Majority Element Solution 思路:构造一个map存储每个数字出现的次数,然后遍历map返回出现次数大于数组一半的数字. 还有一种思路是:对这个数组排 ...
- 169. Majority Element(C++)
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- 23. leetcode 169. Majority Element
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- 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 ...
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- Leetcode#169. Majority Element(求众数)
题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...
- Week1 - 169.Majority Element
这周刚开始讲了一点Divide-and-Conquer的算法,于是这周的作业就选择在LeetCode上找分治法相关的题目来做. 169.Majority Element Given an array ...
- 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore ...
- ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- WinFrom界面框架之WeifenLuo.WinFormsUI.Docking + OutLookBar
本文转载:http://www.cnblogs.com/luomingui/p/3329763.html WeifenLuo.WinFormsUI.Docking + OutLookBar结合使用的效 ...
- 为什么监听不到开机广播action.BOOT_COMPLETED
为什么监听不到开机广播action.BOOT_COMPLETED 1. 说明 Android手机开机后,会发送android.intent.action.BOOT_COMPLETED广播,监听这个广播 ...
- Js Pattern - Namespace Pattern
bad code // BEFORE: 5 globals // Warning: antipattern // constructors function Parent() {} function ...
- HDU 1077Catching Fish(简单计算几何)
Catching Fish Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- [React] Styling React Components With Aphrodite
Aphrodite is a library styling React components. You get all the benefits of inline styles (encapsul ...
- 169 Majority Element [LeetCode Java实现]
题目链接:majority-element /** * Given an array of size n, find the majority element. The majority elemen ...
- springMVC1 springmvc的基础知识
springmvc第一天 springmvc的基础知识 springmvc课程安排: 第一天: 基础知识 springmvc框架(重点) mvc在b/s系统中应用方式 springmvc框架原理(Di ...
- 进程间通信之FIFO
FIFO有时被称为命名管道.管道只能由相关进程使用,这些相关进程的共同祖先进程创建了管道.但是,通过FIFO,不相关的进程也能交换数据. FIFO是一种文件类型(参考http://www.cnblog ...
- 目录操作函数opendir、readdir和closedir
首先,明确一个类型DIR的含义: #include <dirent.h> DIR A type representing a directory stream. DIR是在目录项格式 ...
- Linux添加快捷启动方式 (Ubuntu Debian CentOS)
ubuntu桌面快捷方式的创建 debian添加快捷启动方式 centos 6 桌面建立应用程序的快捷方式的方法 今天用着用着ubuntu,突然抽搐了,感觉特别别扭,特别不方便.新装的应用程序不好 ...