一. 题目描写叙述

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. kvm安装图终端界面及形界面安装系统

    1.图形界面安装: qemu-img create -f qcow2 /kvm/os/vm-01.qcow2 16G mkdir -p /kvm/iso cd /kvm/iso 上传事先下载好的镜像文 ...

  2. Mybatis判断int类型是否为空

     Mybatis判断int是否为空只要!=null就行了  

  3. 【URAL 1486】Equal Squares(二维哈希+二分)

    Description During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued ...

  4. Root of AVL Tree

    04-树5 Root of AVL Tree(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, the ...

  5. JavaScript中离线应用和客户端存储(cookies、sessionStorage、localStorage)

    一.离线应用 所谓离线web应用,就是在设备不能上网的情况下仍然可以运行的应用. 开发离线web应用需要几个步骤:首先,确保应用知道设备是否能上网,以便下一步执行正确的操作:然后,应用还必须能访问一定 ...

  6. poj 1163 数塔

    #include<stdio.h> #include<string.h> #define N 110 int dp[N][N]; int a[N][N]; int Max(in ...

  7. Codeforces Round #292 (Div. 2) D. Drazil and Tiles [拓扑排序 dfs]

    传送门 D. Drazil and Tiles time limit per test 2 seconds memory limit per test 256 megabytes Drazil cre ...

  8. MySQL Slow Log慢日志分析【转】

    如果你的MySQL出现了性能问题,第一个需要“诊断”的就是slow log(慢日志)了. slow log文件很小,使用more less等命令就足够了.如果slow log很大怎么办?这里介绍MyS ...

  9. GOF 23种设计模式目录

    经典的gof 23种设计模式,目录大纲查看. 1. Singleton(单例模式) 保证一个类只有一个实例,并提供访问它的全局访问点. 2. Abstract Factory(抽象工厂模式) 提供一个 ...

  10. FatMouse's Speed--hdu1160(dp+输出路径)

    Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...