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.
需要注意的是:这道题只是要找出多数元素,已经默认存在多数元素了,而不需要去判断是否存在多数元素。之前的思路就一直卡在怎么判断多数元素存的的问题上了。
思路解析:
1. 初始化majorityIndex,并且维护其对应count;
2. 遍历数组,如果下一个元素和当前候选元素相同,count加1,否则count减1;
3. 如果count为0时,则更改候选元素,并且重置count为1;
4. 返回A[majorityIndex]
原理:如果majority元素存在(majority元素个数大于n/2,个数超过数组长度一半),那么无论它的各个元素位置是如何分布的,其count经过抵消和增加后,最后一定是大于等于1的。 如果不能保证majority存在,需要检验。 复杂度:O(N)
Attention: 循环时从i = 1开始,从下一个元素开始,因为count已经置1
C++版:
class Solution {
public:
int majorityElement(vector<int> &num) { int elem = ;
int count = ; for(int i = ; i < num.size(); i++) { if(count == ) {
elem = num[i];
count = ;
}
else {
if(elem == num[i])
count++;
else
count--;
} }
return elem;
}
};
Python版:
class Solution:
# @param {integer[]} nums
# @return {integer}
def majorityElement(self, nums):
lenth=len(nums)
index=0
count=1
for i in range(lenth):
if nums[index]==nums[i]:
count+=1
else:
count-=1
if count==0:
index=i
count+=1
return nums[index]
Majority Element——算法课上的一道题(经典)的更多相关文章
- Reverse Linked List I&&II——数据结构课上的一道题(经典必做题)
Reverse Linked List I Question Solution Reverse a singly linked list. Reverse Linked List I 设置三个指针即可 ...
- Median of Two Sorted Arrays——算法课上经典的二分和分治算法
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- 【算法31】寻找数组的主元素(Majority Element)
题外话 最近有些网友来信问我博客怎么不更新了,是不是不刷题了,真是惭愧啊,题还是在刷的,不过刷题的频率没以前高了,看完<算法导论>后感觉网上很多讨论的题目其实在导论中都已经有非常好的算法以 ...
- leetcode 169. Majority Element 多数投票算法(Boyer-Moore Majority Vote algorithm)
题目: Given an array of size n, find the majority element. The majority element is the element that ap ...
- leetcode 229. Majority Element II(多数投票算法)
就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public: vector<int& ...
- ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java
Given an array of size n, find the majority element. The majority element is the element that appear ...
- leetcode169——Majority Element (C++)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode169:Majority Element(Hash表\位操作未懂)
题目来源: Given an array of size n, find the majority element. The majority element is the element that ...
- leetcode 【 Majority Element 】python 实现
题目: Given an array of size n, find the majority element. The majority element is the element that ap ...
随机推荐
- 使用Google的CDN JQuery库
CDN的全称是Content Delivery Network,即内容分发网络.其目的是通过在现有的Internet中增加一层新的网络架构,将网站的内容发布到最接近用户的网络"边缘" ...
- solr单元测试
package com.taotao.rest.solr; import java.io.IOException; import org.apache.solr.client.solrj.SolrQu ...
- HDU 2646 栈的应用 STL
Expression Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 一个JAVA题引发的思考
转载自:http://www.cnblogs.com/heshan664754022/archive/2013/03/24/2979495.html 十年半山 今天在论坛闲逛的时候发现了一个很有趣的题 ...
- ES6中函数的扩展
一.设置默认参数 ES6之前,给函数设置默认参数是这样做的: function fn(a) { if(typeof y === undefined){ a = a || 'hello'; } cons ...
- Matlab 工具箱介绍
Toolbox工具箱 序号 工具箱 备注 数学.统计与优化 1 Symbolic Math Toolbox 符号数学工具箱 2 Partial Differential Euqation Toolbo ...
- CSS hack浏览器兼容一览表
CSS hack是指我们为了兼容各浏览器,而使用的特别的css定义技巧.这是国外摘来的一张CSS hack列表,显示了各浏览器对css hack的支持程度,对我们制作兼容网页非常有帮助.
- 【NOI】2004 郁闷的出纳员
[算法]平衡树(treap) [题解] treap知识见数据结构. 解法,具体细节见程序. #include<cstdio> #include<algorithm> #incl ...
- NGINX: 反向代理 Nexus
Nginx 反向代理 nexus 的服务, 一直卡在 Initialize... 解决方式是添加一个 header X-Forwarded-Proto: proxy_set_header X-Forw ...
- Commonjs,AMD,CMD和UMD的差异
CommonJS 一种服务器端模块化的规范,Nodejs实现了这种规范,所以就说Nodejs支持CommonJS. CommonJS分为三部分: require 模块加载 exports 模块导出 m ...