LeetCode——Majority Element
在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素。
容易想到的方式就是计数,出现次数最多的就是majority element,其次就是排序,中间的就是majority element。
但是还有两种更有意思的实现方式时间效率O(n),空间效率O(1):
1、Moore voting algorithm 投票算法,因为符合要求的majority element总是存在的,所以首先置计数器count=1,并选择数组的第一个元素作为candidate,往后遍历并计数,与candidate相同则count++,不同则count--,当count=0则选择数组中的下一个数作为candidate,遍历结束candidate则为majority element。
public int majorityElement(int[] num) {
int count = 1;
int candidate = num[0];
for (int i = 1; i < num.length; i++) {
if (count == 0) {
candidate = num[i];
}
if (num[i] == candidate) {
count++;
} else {
count--;
}
}
return candidate;
}
2、这种方式挺有意思。因为majority element出现的次数大于 ⌊ n/2 ⌋ 次,而题目中给的是int类型数组,对于每一个数字的32位,majority element的每一位是相同的,所以不管这一位是1或0,出现次数多的总是majority element的。
下面之所以用C++的方式来实现,是因为在用Java来写的过程中出现了个问题就是Math.pow(a,b)来计算幂次的时候结果是double类型,强转之后丢失一半,所以还要处理这个情况比较麻烦,用C++来没有出现这个问题。
class Solution
{
public:
int majorityElement(vector<int> &num) {
int bitCnt[];
memset(bitCnt, , sizeof(bitCnt)); for (int i = ; i < num.size(); i++) {
for (int j = ; j < ; j++) {
if (num[i] & ( << j))
bitCnt[j]++;
}
} int ans = ;
for (int i = ; i < ; i++) {
if (bitCnt[i] > num.size()/)
ans += (int)pow(, i);
}
return ans;
}
};
LeetCode——Majority Element的更多相关文章
- 2016.5.18——leetcode:Majority Element
Majority Element 本题收获: 1.初步了解hash,nth_element的用法 2.题目的常规思路 题目: Given an array of size n, find the ma ...
- [LeetCode] Majority Element II 求众数之二
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- [LeetCode] Majority Element 求众数
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode Majority Element I && II
原题链接在这里:Majority Element I,Majority Element II 对于Majority Element I 来说,有多重解法. Method 1:最容易想到的就是用Hash ...
- [LeetCode] Majority Element II 求大多数之二
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...
- [LeetCode] Majority Element 求大多数
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode Majority Element I
原题链接在这里:https://leetcode.com/problems/majority-element/ 题目: Given an array of size n, find the major ...
- LeetCode Majority Element Python
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Leetcode: Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
随机推荐
- win7 32/64bit VS2010 OpenCV 2.4.9 环境配置
作者 : 卿笃军 因为近期人脸识别须要用到OpenCV,让我開始了OpenCV学习. OpenCV 2.4.9下载地址:http://sourceforge.net/projects/opencvli ...
- opengl 函数
( 7 )光栅化.象素操作函数. 像素位置 glRasterPos*() .线型宽度 glLineWidth() .多边形绘制模式 glPolygonMode() ,读取象素 glReadPixel( ...
- mysql @变量和变量的区别及怎么判断记录唯一性
DELIMITER// drop PROCEDURE if EXISTS test.express; create PROCEDURE test.express() BEGIN ) into @a f ...
- RT: TCP REUSEADDR or REUSEPORT
Welcome to the wonderful world of portability... or rather the lack of it. Before we start analyzing ...
- android中的数据库操作(转)
android中的数据库操作 android中的应用开发很难避免不去使用数据库,这次就和大家聊聊android中的数据库操作. 一.android内的数据库的基础知识介绍 1.用了什么数据库 an ...
- Linux入门之——安装虚拟机软件
/** ****************************************************************************** * @author 暴走的小 ...
- js select 实现左右传值.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- CSS 布局Float 【1】
1.HTML元素分类 HTML元素大题可分为内联(inline)元素和块(block)元素. 1.1 内联元素(inline) ①元素显示方式:"文本方式",1个挨着1个,不独自占 ...
- Python datetime time 常用操作
测试版本: Python 2.7 获取当前时间的两种方法 import datetime,time now = time.strftime("%Y-%m-%d %H:%M:%S") ...
- 利用xampp进行https操作
环境:win7 32位 安装xampp: 一般情况Apache的端口号可能会冲突,建议修改.修改方法如下: 1.点击Config