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的更多相关文章

  1. 169. Majority Element(C++)

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  2. 23. leetcode 169. Majority Element

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  3. 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 ...

  4. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  5. Leetcode#169. Majority Element(求众数)

    题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...

  6. Week1 - 169.Majority Element

    这周刚开始讲了一点Divide-and-Conquer的算法,于是这周的作业就选择在LeetCode上找分治法相关的题目来做. 169.Majority Element Given an array ...

  7. 169. Majority Element - LeetCode

    Question 169. Majority Element Solution 思路:构造一个map存储每个数字出现的次数,然后遍历map返回出现次数大于数组一半的数字. 还有一种思路是:对这个数组排 ...

  8. leetcode——169 Majority Element(数组中出现次数过半的元素)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  9. ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  10. (Array)169. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

随机推荐

  1. 读书笔记---改善c#编程的157个建议

    1.在拼接string时,如果牵涉到其他类型,先tostring一下会减少装箱操作:频繁操作字符串变量的话,使用stringbuilder效率较高. 2.tryParse相对于parse而言效率高,t ...

  2. Ubuntu 制作离线安装包

    2017-04-07 11:39:59 一.应用场景 Ubuntu在不能上网情况下,又需要安装软件或更新系统 二.离线安装包制作 在一台能正常上网的Ubuntu系统上,安装需要的软件包 $sudo a ...

  3. Java 8 Stream API

    Java 8 Stream API JDK8 中有两大最为重要的改变.第一个是 Lambda 式:另外 Stream API(java.util.stream.*) Stream 是 JDK8 中处理 ...

  4. 2018.10.19 NOIP模拟 硬币(矩阵快速幂优化dp)

    传送门 不得不说神仙出题人DZYODZYODZYO出的题是真的妙. f[i][j][k]f[i][j][k]f[i][j][k]表示选的硬币最大面值为iii最小面值不小于jjj,总面值为kkk时的选法 ...

  5. 2018.09.26洛谷P1084 疫情控制(二分+倍增)

    传送门 好题啊. 题目要求的最大值最小,看到这里自然想到要二分答案. 关键在于怎么检验. 显然对于每个点向根走比向叶节点更优. 因此我们二分答案之后,用倍增将每个点都向上跳到跳不动为止. 这时我们ch ...

  6. Django(4)

    https://www.cnblogs.com/yuanchenqi/articles/7439088.html

  7. 转一个Visual Stuido 快捷键

    http://www.shortcutworld.com/en/win/Visual-Studio_2010.html 快捷键分类很详细.

  8. 日历时间选择控件---3(支持ie、火狐)

    效果展示:  源代码: <script language=javascript ><!--/* 调用方法:不能用onfocus,要用onclick  <input onclic ...

  9. HDU 2037 今年暑假不AC (区间贪心)

    题意:又是中文题... 析:先说一下区间贪心的一个定理,选择不相交的区间:数轴上有n个开区间(ai, bi).选择尽量多的区间,使得这些区间两两不相交,贪心策略是,一定是选bi小的.(想一下为什么). ...

  10. 使用Revel(go)开发网站

    Revel很好的利用了Go语言的goroutine,把每一个request都分配到了goroutine里.不用再写一大堆的回调.如果你写过nodejs的话就会深刻的体会到callback hell是什 ...