《leetCode》:Majority Element II
2016-03-27 17:36
267 查看
题目
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.
思路
这个题不会做也,下面思路来源于:https://leetcode.com/discuss/82195/quick-select-c-solutionThe idea is to split the array into three parts according to the selected pivot: left, middle and right. Let's say we have two indices m and n, so that all elements in [0 ... m-1] are less than the pivot, elements in [m...n] are equal to the pivot (two ends inclusive), [n+1 ... end] contains elements greater than the pivot. Then there are some facts: if n - m + 1 >= 1 + nums.size()/3, nums[m] must be added to the results. If m - 1 < 1 + nums.size()/3, we can simply abandon the left part, otherwise we have to consider it. if nums.size()-n < 1+nums.size()/3, the right part can be dropped, otherwise it has to be checked. Ideally, we can drop about 1/3 of the array each time, so the running time is something like: 1 + 2/3 + (2/3)*(2/3) + ... = (1-(2/3)^k)/(1-2/3), O(n). A big advantage of this algorithm is that we can simply apply it to 1/4,1/5 ...
实现代码如下:
/** * Return an array of size *returnSize. * Note: The returned array must be malloced, assume caller calls free(). */ int *res=NULL; int indexRes=0; void swap(int *a,int *b){ int temp=*a; *a=*b; *b=temp; } void majorityElementHelper(int *nums,int left,int right,int len){ if(nums==NULL||right<left){ return; } //选取mid位置的值作为pivot值 int mid=(left+right)/2; int val=nums[mid]; int m=left;//用来指示 调整后的结果中,m左边的值都是小于val的。 int n=left;//用来指示 调整后的结果中,m~n中的值都是等于val的。n~right都是大于val的 swap(nums+left,nums+mid);//将pivot放入nums第一个位置 int i=left+1; while(i<=right){ if(nums[i]<val){ swap(&nums[m++],&nums[i]);//将小于val的值放入m下标的左边 swap(&nums[++n],&nums[i++]);//使得n总是指向最右边的等于val的下标 } else if(nums[i]==val){ swap(&nums[++n],&nums[i++]); } else{ i++; } } if(n-m+1>=len){//符合条件 res[indexRes++]=nums ; } if(m-left>=len){//小于val的长度大于len,因此可能有解 majorityElementHelper(nums,left,m-1,len); } if(right-n>=len){//大于val的长度大于len,因此可能有解 majorityElementHelper(nums,n+1,right,len); } } int* majorityElement(int* nums, int numsSize, int* returnSize) { if(nums==NULL||numsSize<1){ *returnSize=0; return NULL; } int maxLen=3;//最多只有三个解 res=(int *)malloc(maxLen*sizeof(int)); if(res==NULL){ exit(EXIT_FAILURE); } indexRes=0; majorityElementHelper(nums,0,numsSize-1,(numsSize/3)+1); *returnSize=indexRes; return res; }
相关文章推荐
- POJ 3280 Cheapest Palindrome(区间DP)
- 关于国内外CV领域牛人的博客链接
- c++上机作业-2
- 第四周学习进度
- >hibernate.cfg.xml的一些常用配置
- Html5全屏_IE
- C#-readonly与const不同
- code forces 630 P. Area of a Star
- 【java 基础】Windows dos 命令
- Html5全屏_Chrome
- 关于在c#中如何遍历listbox中选中项的问题
- HDOJ 2041 超级楼梯
- Android ORM应用开发框架KJFrameForAndroid使用详解
- Java基础之Map实现篇
- Html5全屏_FireFox
- Android初试--Android中的ContentProvider(3)
- apache服务的基本概念(DSO、MPM)
- 获取网络状态
- fast-rcnn配置运行VGG16.caffemodel和VGG_CNN_M_1024.v2.caffemodel(Ubuntu14.04)
- Linux内核分析学习笔记:system_call中断处理过程