[Java] Majority Element II 找众数2

leetcode 链接 :https://leetcode.com/problems/majority-element-ii/

题目

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.

简要翻译:

就是给定一个含有n个数的数组,找到所有出现次数大于⌊ n/3 ⌋次的数。算法要求线性时间复杂度O(N),空间复杂度O(1).

此题如果采用先排序在去查找的话,显然是不能满足时间复杂度的,因为基于比较的排序最快也要O(N*logN)

根据我的上一篇博客(http://blog.csdn.net/lyy_hit/article/details/47616641)的解题思路,我们是可以在O(N)的时间复杂度解决问题的。

首先我们要分析,到底有几个数是满足条件的。很显然可能的情况就是0或1或2个数,为什么大于3就不行呢? 根据反正法是可以证明的,此处省略

在实现的过程中呢,我们可以假设存在两个数是满足条件的,用两个临时变量来存储,找到这两个数之后,因为可以证明若存在,必定就是临时变量存储的这两个数。因此我们可以再次遍历一次数组来统计出现的次数,若满足条件则是所要找的数,若不满足则不是。

代码实现如下:

package com.easy;

import java.util.ArrayList;
import java.util.List;

public class MajorityElementII
{
	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		int []nums = {2, 2};//{-1, 1, 1, 1, 2, 1};//{1, 3, 1, 2, 3, 4, 2, 2, 3, 3, 2};
		List<Integer> list = majorityElement(nums);
		
		for (int i = 0; i < list.size(); i++)
		{
			System.out.println(list.get(i));
		}
	}
	public static List<Integer> majorityElement(int[] nums) {
        List<Integer> list = new ArrayList<Integer>();
        int A = 0, B = 0;
        int cntA = 0, cntB = 0;
        for (int i = 0; i < nums.length; i++)
        {
        	if (cntA == 0 && cntB == 0)
        	{
        		A = nums[i]; cntA++;
        	}else if (cntA > 0 && cntB == 0)
        	{
        		if (A != nums[i])
        		{
        			B = nums[i]; cntB++;
        		}else
        		{
        			cntA++;
        		}
        	}else if (cntA == 0 && cntB > 0)
        	{
        		if (B != nums[i])
        		{
        			A = nums[i]; cntA++;
        		}else
        		{
        			cntB++;
        		}
        	}else
        	{
        		if (A == nums[i])
        		{
        			cntA++;
        		}else if (B == nums[i])
        		{
        			cntB++;
        		}else
        		{
        			cntA--; cntB--;
        		}
        	}
        	
//        	System.out.println("A = "+A+" cntA = " + cntA + " B = " + B + " cntB = " + cntB);
        }
        if (cntA > 0)
        {
        	cntA = 0;
        	for (int i = 0; i < nums.length; i++)
        	{
        		if (A == nums[i])
        		{
        			cntA++;
        		}
        	}
        	if (cntA > nums.length/3)
        	{
        		list.add(A);
        	}
        }
        if (cntB > 0)
        {
        	cntB = 0;
        	for (int i = 0; i < nums.length; i++)
        	{
        		if (B == nums[i])
        		{
        			cntB++;
        		}
        	}
        	if (cntB > nums.length/3)
        	{
        		list.add(B);
        	}
        }
        return list;
    }
}

ps:在实现过程中要注意分情况,不能漏掉任何一种情况

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值