Divide and Conquer-169. 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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
int majorityElement(int* nums, int numsSize) {
if(numsSize == ||numsSize == )
return *nums;
int x = majorityElement(nums,numsSize / );
int y = majorityElement(nums + numsSize / ,numsSize - numsSize / );
if(x == y)
return x;
else
{
int countX = ;
int countY = ;
for(int i = ;i < numsSize;i++)
{
if(*(nums + i) == x)
countX++;
else if(*(nums + i) == y)
countY++;
}
return countX > countY ? x : y;
}
}
Divide and Conquer-169. Majority Element的更多相关文章
- 169. Majority Element(C++)
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- 23. leetcode 169. Majority Element
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II
169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...
- 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(求众数)
题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...
- Week1 - 169.Majority Element
这周刚开始讲了一点Divide-and-Conquer的算法,于是这周的作业就选择在LeetCode上找分治法相关的题目来做. 169.Majority Element Given an array ...
- 169. Majority Element - LeetCode
Question 169. Majority Element Solution 思路:构造一个map存储每个数字出现的次数,然后遍历map返回出现次数大于数组一半的数字. 还有一种思路是:对这个数组排 ...
- 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 求出现次数最多的数 --------- java
Given an array of size n, find the majority element. The majority element is the element that appear ...
- (Array)169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- 教你如何制作饼干icon教程
Hello,不露又和大家见面了,今天给大家带来的是一个可爱Q弹的icon~ 看起来像块饼干是吧~ 做起来非常简单哦,快打开PS一起躁起来吧. 先来看看效果图: 步骤1:打开PS,新建一个800*600 ...
- event对象的理解
0.给对象绑定事件准确的说是给对象事件绑定事件函数 1.event:事件对象,当一个事件发生的时候,和当前这个对象发生的事件有关的信息都会被i临时保存到event对象中 2.event对象必须在一个事 ...
- 20155213 2016-2017-2 《Java程序设计》第八周学习总结
20155213 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 第十四章NIO与NIO2 NIO NIO使用频道来衔接数据节点,在处理数据时,NIO可以让你 ...
- 633. Sum of Square Numbers
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- EXCEL 单元格引用问题
=(SUM(INDIRECT("'2.5酒店预订收入'!"&"J"&MATCH(C21,'2.5酒店预订收入'!B:B,0)&" ...
- bootstrap-treeview的 简单使用
理论:http://blog.csdn.net/babyxue/article/details/73835444 插依赖Bootstrap 和jQuery <link href="~/ ...
- 如何设置vim中tab键缩进---配置初始化设置
转载自:http://blog.51cto.com/xuding/1725376:加了一些补充说明 问题: Linux系统下,Tab键默认为8个字符,需呀将其修改为4个字符的方式使用 步骤: 1.在用 ...
- 测试setsockopt设置超时是否生效代码
// 测试setsockopt设置超时是否生效代码 #include <arpa/inet.h> #include <netinet/in.h> #include <st ...
- Spark-1.2.2部署
1.安装Scala 1.1解压和安装 在Scala官网http://www.scala-lang.org/download/下载Scala安装包,然后解压.(注:JDK的版本最好是1.7及以上,否则S ...
- hdu 5024 最长的L型
http://acm.hdu.edu.cn/showproblem.php?pid=5024 找到一个最长的L型,L可以是斜着的 简单的模拟 #include <cstdio> #incl ...