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.
int majorityElement(int* nums, int numsSize)
{
if(numsSize==)
return nums[]; int i,j,k;
for(i=;i<numsSize;i++)
{
for(j=i-;j>=;j--)
{
if(nums[i]>=nums[j])
break;
} if(i!=j-)
{
int temp=nums[i];
for(k=i-;k>j;k--)
{
nums[k+]=nums[k];
}
nums[k+]=temp;
}
} int Time;
Time=numsSize/;
int count=;
for(i=;i<numsSize-;i++)
{
if(nums[i]==nums[i+])
{
count++;
if(count>Time)
{
return nums[i];
}
}
else
{
count=;
}
} return ;
}
LeeCode-Majority Element的更多相关文章
-
[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
题目概述: Given an array of size n, find the majority element. The majority element is the element that ...
-
✡ 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 ...
-
LeetCode 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
-
[UCSD白板题] Majority Element
Problem Introduction An element of a sequence of length \(n\) is called a majority element if it app ...
-
Leetcode # 169, 229 Majority Element I and II
Given an array of size n, find the majority element. The majority element is the element that appear ...
-
LeetCode【169. Majority Element】
Given an array of size n, find the majority element. The majority element is the element that appear ...
-
【10_169】Majority Element
今天遇到的题都挺难的,不容易有会做的. 下面是代码,等明天看看Discuss里面有没有简单的方法~ Majority Element My Submissions Question Total Acc ...
随机推荐
-
The file couldn&#39;t be opened because you don&#39;t have permission to view it
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
-
uvalive 3890 Most Distant Point from the Sea
题意:求一个凸多边形中一点到边的最大距离. 思路:转换成在多边形内部,到每边距离为d的直线所围成的内多边形是否存在.也就是,二分距离+半平面交. #include<cstdio> #inc ...
-
iOS 相互引用引起内存泄露问题说明
release动作只会对自身计数减一,而不会对属性发出release消息,只有该对象的引用计数为0,系统才会对该对象的所有属性发出release消息 类A的属性有类B,类B的属性有类A,而且两者都是强 ...
-
RMAN 备份
backup database; --备份整库 backup database format '\xxxxxx\xxx_%U'; --备份整库到指定路劲 backup tablespace users ...
-
nyoj 题号12 水厂(两)——南阳oj
标题信息: 喷水装置(二) 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描写叙述 有一块草坪.横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=1000 ...
-
duilib 新增数据迁移界面
xml界面配置: <?xml version="1.0" encoding="utf-8"?> <Window caption="0 ...
-
python测试开发django-50.jquery发送ajax请求(get)
前言 有时候,我们希望点击页面上的某个按钮后,不刷新整个页面,给后台发送一个请求过去,请求到数据后填充到html上,这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新.Ajax可以完美的 ...
-
Objective-C中,类方法的getter和setter可以用点运算符吗?
Objective-C中,对象实例property的getter和setter可以使用点运算符来操作,那么类方法的getter和setter可以使用点运算吗? 答案是肯定的. 看如下代码: #impo ...
-
[HDU - 5408] CRB and Farm
题意: 给出一个由n个点组成的凸包,以及凸包内的k个点,问是否能够在凸包上选择最多2k个点构造一个新的 凸包,使得新的凸包覆盖原来的k个点. 要求2k个点覆盖原本的k个点,只要对原k个点构造凸包,然后 ...
-
UnicodeDecodeError: &#39;ascii&#39; codec can&#39;t decode byte 0xbb in position 51: ord
1.问题描述:一个在Django框架下使用Python编写的定时更新项目,在Windows系统下测试无误,在Linux系统下测试,报如下错误: ascii codec can't decode byt ...