一. 题目描写叙述

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.

二. 题目分析

题目说到,给定一个数组。内含n个元素。找出一个主元素,该元素在数组中出现的次数比其它元素出现的次数加起来还要多。即该元素的数量大于n/2

開始的思路,自然是对数组进行排序,数组最中间的元素就是主元素,但算法复杂度一般须要:O(nlogn);若同意辅助内存。可新建一个数组。用于存放不同元素值在数组中存放的次数。这样仅仅需遍历一次原数组,然后再遍历一次记录次数的数组就可找出主元素。算法复杂度为O(n)

一种高效的方法仅仅需遍历一次数组就可以。

我们知道主元素出现的次数比其它元素出现的次数和还要大,因此,仅仅需使用两个变量:candidatecount这两个变量记录了元素candidate的值,count记录了元素candidate比其它元素多出现的次数。

遍历数组,遇到与当前记录元素candidate同样的元素值,++count;否则count被抵消一次。--count。当count变为0时,更换candidate为当前遍历所在的像素值。

由于遍历完数组后,主元素被抵消的次数count比然大于零,不会由于当count变为0而被其它数组元素替代。因此candidate也仅仅会是主元素。

三. 演示样例代码

class Solution {
public:
int majorityElement(vector<int>& nums) {
int candidate = 0, 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;
}
};

leetcode笔记:Majority Element的更多相关文章

  1. [LeetCode] 169. Majority Element 多数元素

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

  2. [LeetCode] 229. Majority Element II 多数元素 II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...

  3. LeetCode 169. Majority Element (众数)

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

  4. leetcode 169 Majority Element 冰山查询

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

  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 - majority vote algorithm (Java)

    1. 题目描述Description Link: https://leetcode.com/problems/majority-element/description/ Given an array ...

  7. 【leetcode】Majority Element

    题目概述: Given an array of size n, find the majority element. The majority element is the element that ...

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

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

  9. LeetCode 169. Majority Element

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

  10. Java for LeetCode 169 Majority Element

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

随机推荐

  1. day02五大运算符:逻辑运算符、成员运算符、算数、比较、赋值、

    # -*- encoding: utf-8 -*-# print('hello 中国')# 变量# print(10 + 20 + 3 + 15)# print((10 + 20 + 3 + 15)* ...

  2. 【简●解】[AHOI2009]中国象棋

    [题目大意] 叫你在\(n×m\)的棋盘上放若干个炮(可以是0个),使得没有一个炮可以攻击到另一个炮,问有多少种放置方法. [关键词] \(DP\) 分类讨论 乘法和加法原理 [分析] 仔细观察就会发 ...

  3. 牛客网NOIP赛前集训营-提高组(第二场)A 方差

    链接:https://www.nowcoder.com/acm/contest/173/A来源:牛客网 题目描述 一个长度为 m 的序列 b[1...m] ,我们定义它的方差为 ,其中  表示序列的平 ...

  4. [LUOGU] P3871 [TJOI2010]中位数

    题目描述 给定一个由N个元素组成的整数序列,现在有两种操作: 1 add a 在该序列的最后添加一个整数a,组成长度为N + 1的整数序列 2 mid 输出当前序列的中位数 中位数是指将一个序列按照从 ...

  5. Django 再次学习笔记整理

    url 路由系统 urlpatterns = [ # path('admin/', admin.site.urls), path('index/', views.index), re_path('^e ...

  6. 大数据学习——面试用sql——累计报表

    create table t_access_times(username string,month string,salary int)row format delimited fields term ...

  7. 81. Spring Boot集成JSP疑问【从零开始学Spring Boot】

    [原创文章,转载请注明出处] 针对文章: ()Spring Boot 添加JSP支持[从零开始学Spring Boot] 有网友提了这么一些疑问: 1.Spring Boot使用jsp时,仍旧可以打成 ...

  8. 80. Hibernate 5.0命名策略使用naming-strategy 不起作用【从零开始学Spring Boot】

    [原创文章,转载请注明出处] 事情的起因:一不小心从1.3.3升级到了1.4.0版本,结果就碰到了各种悲催的事情了,好吧,Hibernate5.0的新特性就是其中一个坑,我们会发现我们配置的namin ...

  9. 使用RMAN恢复数据库

    使用RMAN恢复数据库 由于需要搭建一个测试环境,把上周末的一个全备拿出来做恢复,首先备份一下测试库上现有的重要表: exp banping/bangping@ERPTEST file=f:\2009 ...

  10. 【二分图匹配】E. 过山车

    https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/E [题意] 裸的最大匹配 [教训] 一开始边数开了k,建的是无向图,结果T了,改 ...