leetcode笔记:Majority Element
一. 题目描写叙述
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)。
一种高效的方法仅仅需遍历一次数组就可以。
我们知道主元素出现的次数比其它元素出现的次数和还要大,因此,仅仅需使用两个变量:candidate和 count这两个变量记录了元素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的更多相关文章
- [LeetCode] 169. Majority Element 多数元素
Given an array of size n, find the majority element. The majority element is the element that appear ...
- [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 ...
- LeetCode 169. Majority Element (众数)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- leetcode 169 Majority Element 冰山查询
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 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 - majority vote algorithm (Java)
1. 题目描述Description Link: https://leetcode.com/problems/majority-element/description/ Given an array ...
- 【leetcode】Majority Element
题目概述: Given an array of size n, find the majority element. The majority element is the element that ...
- ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Java for LeetCode 169 Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- dns config
.: { forward . { force_tcp expire 50s health_check 50s } cache debug errors whoami log } Corefile .: ...
- windows10下Anaconda的安装与tensorflow、opencv的安装与环境配置
刚开始学习tensorflow和opencv这一块的知识,所以用博客这个平台来把自己这段学习的经历与感想写下来. tensorflow和opencv则用Anaconda来下载和配置环境. 下载Anac ...
- Spring MVC 接入 rabbitMQ
依赖包 <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spr ...
- JSTL标签判断list是否为空
jsp页面判断获得action传的list的是否为空或者list.size的长度,就可以用fn这个标签: <c:if test="${list== null || fn:length( ...
- LeetCode(101)Symmetric Tree
题目 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). Fo ...
- 关于一个多个.cpp文件的项目中,函数出现未定义引用错误
编译的话,必须把全部的c文件都要编译的啊,只编译一个c算怎么个逻辑呢?编译实际上是2个过程,编译和链接.编译过程只检查所有的符号(变量,函数)有没有声明,即只需要h文件生命就够了.但是链接时候,需要找 ...
- JavaScript中离线应用和客户端存储(cookies、sessionStorage、localStorage)
一.离线应用 所谓离线web应用,就是在设备不能上网的情况下仍然可以运行的应用. 开发离线web应用需要几个步骤:首先,确保应用知道设备是否能上网,以便下一步执行正确的操作:然后,应用还必须能访问一定 ...
- 关于对象字面量花括号{} key的类型
原来对{}方式建立的实例化后的对象的key,理解不清,只知道,数组加不加“”,反正都是字符,现在要理解下,到底怎么回事?返回结果是 var a={}; a[1]="a"; a[&q ...
- zoj 1251 Box of Bricks
Box of Bricks Time Limit: 2 Seconds Memory Limit: 65536 KB Little Bob likes playing with his bo ...
- Go map例题
package main import "fmt" //map例题 //寻找最长不含有重复字符的子串 // abcabcbb -> abc //pwwkew ->wke ...