题目:主元素 II

来源:互联网 发布:uml类图转化为java代码 编辑:程序博客网 时间:2024/06/05 18:02
给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一。




您在真实的面试中是否遇到过这个题?

Yes





样例

给出数组[1,2,1,2,1,3,3] 返回 1

注意

数组中只有唯一的主元素


挑战

要求时间复杂度为O(n),空间复杂度为O(1)。

标签 Expand   



相关题目 Expand   



解题思路:

三三抵销法,但是也有需要注意的地方:

1. 我们对cnt1,cnt2减数时,相当于丢弃了3个数字(当前数字,candidate1, candidate2)。也就是说,每一次丢弃数字,我们是丢弃3个不同的数字。

而Majority number超过了1/3所以它最后一定会留下来。

设定总数为N, majority number次数为m。丢弃的次数是x。则majority 被扔的次数是x

而m > N/3, N - 3x > 0. 

3m > N,  N > 3x 所以 3m > 3x, m > x 也就是说 m一定没有被扔完

最坏的情况,Majority number每次都被扔掉了,但它一定会在n1,n2中。

2. 为什么最后要再检查2个数字呢(从头开始统计,而不用剩下的count1, count2)?因为数字的编排可以让majority 数被过度消耗,使其计数反而小于n2,或者等于n2.前面举的例子即是。

另一个例子:

1 1 1 1 2 3 2 3 4 4 4 这个 1就会被消耗过多,最后余下的反而比4少。
参考http://www.cnblogs.com/EdwardLiu/p/4331430.html
public class Solution {    /**     * @param nums: A list of integers     * @return: The majority number that occurs more than 1/3     */    public int majorityNumber(ArrayList<Integer> nums) {        // write your code            int i = 0;          int can1 = nums.get(0);          int count1 = 1;          i++;          int can2  = 0;          int count2 = 0;          if(i<nums.size()&&nums.get(i)!=can1){               can2 = nums.get(i);               count2 = 1;               i++;          }          for(int j= i;j<nums.size();j++){               if(can1 == nums.get(j)){                    count1++;               }else if(can2 == nums.get(j)){                    count2++;               }else{                    if(count1==0){                         can1 = nums.get(j);                         count1++;                    }else if(count2==0){                         can2 = nums.get(j);                          count2++;                    }else{                         count1--;                         count2--;                    }                                 }                        }          count1 = 0;          count2 = 0;          for(int k=0;k<nums.size();k++){               if(nums.get(k)==can1){                    count1++;               }               if(nums.get(k)==can2){                    count2++;               }          }          return count1>count2?can1:can2;    }}



0 0
原创粉丝点击