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 ...
随机推荐
- 设计模式-结合Android代码
开始学设计模式 1 单例模式 单例模式可以说是最容易理解的模式了,也是应用最广的模式之一,先看看定义吧. 定义:确保单例类只有一个实例,并且这个单例类提供一个函数接口让其他类获取到这个唯一的实例. 什 ...
- Mybatis学习之配置文件
Mybatis也是ORM框架的一种,与Hibernate框架的不同就是Hibernate框架是实体与表的映射,是一种全自动的ORM实现,而Mybatis是实体与sql语句的映射,是一种半自动的ORM映 ...
- DataSource , DataSink, DataSourceLoop
Script assertion in login:
- PL/pgSQL的 RETURN NEXT例子
从网上找到例子: 可以说,RETURN NEXT要用在循环中: 例子一: 数据准备: CREATE TABLE foo (fooid INT, foosubid INT, fooname TEXT); ...
- Android访问WebService的两种方法
首先解释一下WebService:WebService是一种基于SOAP协议的远程调用标准.通过WebService可以将不同操作系统平台,不同语言.不同技术整合到一起.详细见:http://baik ...
- Codeforces Round #313 (Div. 2) B. Gerald is into Art 水题
B. Gerald is into Art Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/560 ...
- 使用C# 实现文件锁
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 【VMware混合云】应用为王
作者:范军 (Frank Fan) 新浪微博:@frankfan7 微信:frankfan7 你可能会问,VMware混合云不是IaaS么,怎么说起应用了? 如果我们回到原始的问题,搭建Infra ...
- [AngularJS] Using ngModel in Custom Directives
You can use ngModel in your own directives, but there are a few things you'll need to do to get it w ...
- 调取jSon数据--展示
下面代码是将页面中的展示部分 function searchProductlistByfilterCondition(index, type, sort, filterWord) { //cite_h ...