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 ...
随机推荐
- DB2与oracle类型对比
本文摘自http://www.cnblogs.com/cy163/archive/2010/11/17/1880280.html 做过DB2数据库应用迁移的工程师,了解IBM MTK工具在迁移过程中所 ...
- 什么是@guid
我将给大家讲解史上最通俗一同guid与ID的区别...
- 抽象 abstract 和 接口 interface。 java 的 堆 和 栈。 参数传递(基本类型和普通对象的区别)
package com.test; import com.test.Pro; //protected 修饰的成员只能在本包中和 继承该类的方法中使用 public abstract class Tes ...
- 上海第八中学 shader
http://shiba.hpe.cn/jiaoyanzu/wuli/soft/xna.aspx?classId=4
- 冒泡排序java语言实现
class bubbleSort { public static void main(String[] args){ int[] a={49,38,65,97,76,13,27,49,78,34,12 ...
- 将Tomcat设置为自动启动的服务最快捷方法
将Tomcat设置为自动启动的服务: 最近遇到了个问题,服务器上的项目突然访问不了,就上服务器去重启了tomcat服务,谁知道到最后tomcat的服务报错了,重新启动服务的选项 也没有,之前这个项目也 ...
- Python特殊方法
# __slots__如果要限制添加的属性,例如,Student类只允许添加 name.gender和score 这3个属性,就可以利用Python的一个特殊的__slots__来实现. # __sl ...
- IntelliJ IDEA 2017版 springloaded实现热部署
1.配置pom.xml文档 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...
- 创建template模板
创建一个自己常用的快捷键的模板. 1.选择window-->preferrence--搜索temp--java 2.点击new 3.根据自己的习惯起一个名字 4.比如syso 5.语法不会,就找 ...
- DIV+CSS 中的 overflow:hidden
overflow:hidden这个CSS样式是大家常用到的CSS样式,但是大多数人对这个样式的理解仅仅局限于隐藏溢出,而对于清除浮动这个含义不是很了解. 一提到清除浮动,我们就会想到另外一个CSS样式 ...