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 ...
随机推荐
- vector去除重复的元素
vector<int> v; sort(v.begin(),v.end()); v.erase(unique(v.begin(), v.end()), v.end());
- 《python核心编程》--读书笔记 第21章 数据库编程
准备:今天拿笔记本装了mysql,这样就能在不同地方用其他电脑远程访问同一个数据库了. python安装MySQLdb模块:http://www.codegood.com/downloads. 21. ...
- oepncv-学习笔记一
安装opencv文件时若需要cmake编译,如果中间出现 解决办法是: 在opencv的文件中找到包含cmakelist.txt的文件夹,把where is the source code:的路径改成 ...
- ListBox, ListView, GridView
ListView是ListBox的派生类,而GridView是ViewBase的派生类 ListView的View属性是ViewBase,所以GridView可以作为ListView的属性 如 < ...
- php环境的安装
一.xampp的安装 1.下载xampp安装包. 2.next下一步傻瓜式的安装. 3.输入地址127.0.0.1进入如下页面. 二.LAMP环境的安装
- centos设置tomcat开机启动
1.编辑开机启动脚本 vi /etc/init.d/tomcat8 #!/bin/bash # tomcat8:start|stop|restart # chkconfig: 345 90 10 # ...
- jsp 内置对象(一)
一.jsp的九大内置对象 内置对象 所属类 pageContext javax.servlet.jsp.PageContext request javax.servlet.http.HttpServl ...
- jsp03( javabeans)
1.javabean简介 Javabeans就是符合某种特定规范Java类.使用Javabeans的好处是[解决代码的重复编写],减少代码冗余,功能区分明确,提高代码的维护性. 2.javabean的 ...
- C# 如何用多字符分割字符串
用单字符分割字符串大家应该很熟悉,例如: string source = "dfd^Afdf^AAAAAA^Adfdf"; var list= source.Split('A'); ...
- Winform MD5
1:MD5 http://www.cmd5.com/ 字节数组----字符串 //将字节数组中每个元素按照指定的编码格式解析成字符串//直接将数组ToString()//将字节数组中的每个元素ToSt ...