一. 题目描写叙述

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. django 127.0.0.1 将您重定向的次数过多

    "GET /?next=/%3Fnext%3D/%253Fnext%253D/ HTTP/1.1" 302 0 solution reference from django.con ...

  2. CreateProcess相关

    CreateProcess不创建窗口执行: https://blog.csdn.net/rongwenbin/article/details/24422041 CreateProcess返回值: 执行 ...

  3. UVa-133-救济金发放

    这题的话,我们首先对于移动函数可以知道,因为只是顺逆的关系,也就是加一或者减一,所以我们每次移动的时候,都补上一个小于n的最大整数,然后取模,这样就不会有负数,而且加之后的结果不会超过2*n,所以我们 ...

  4. [LUOGU] P3205 [HNOI2010]CHORUS 合唱队

    为了在即将到来的晚会上有更好的演出效果,作为AAA合唱队负责人的小A需要将合唱队的人根据他们的身高排出一个队形.假定合唱队一共N个人,第i个人的身高为Hi米(1000<=Hi<=2000) ...

  5. Oracle 11G RAC 修改IP

    实验环境 类别 修改前 修改后 PUBLIC 172.18.4.182 rac1 192.168.56.10 rac1 172.18.4.184 rac2 192.168.56.20 rac2 PRI ...

  6. Django框架基础知识07-常用查询及表关系的实现

    1.常用的模型字段类型 https://docs.djangoproject.com/en/2.1/ref/models/fields/#field-types 2.字段的常用参数 官方文档:http ...

  7. 【HIHOCODER 1575】 两个机器人(BFS)

    描述 一个N × M的2D迷宫中有两个机器人.机器人A在迷宫左上角,只能向右或向下移动:机器人B在迷宫右下角,只能向左或向上移动.机器人不能移动到迷宫外.此外,由于奇怪的同步机制,这两个机器人只能同时 ...

  8. nginx.conf的完整配置说明

    #用户 用户组 user www www; #工作进程,根据硬件调整,有人说几核cpu,就配几个,我觉得可以多一点 worker_processes 5: #错误日志 error_log logs/e ...

  9. java环境配置——jdk8

    在官网下载最新版本的jdk 测试版本:jdk-8u60-windows-x64.exe 测试环境:Windows Server 2012 R2 Standard  X64 开始执行安装 安装过程中会选 ...

  10. hexo干货系列:(七)hexo安装统计插件

    前言 前面介绍了如何让百度和谷歌收录我们的博客,那如何查看自己的博客每天被多少人访问呢~ 这里我介绍下hexo中如何使用统计插件,每天看到自己的博客访问量越来越高也是一种享受. 正文 开启统计功能 我 ...