java实现找出数组中出现次数超过一半的数字

本文介绍两种高效算法来查找数组中出现次数超过一半的数字。一种基于快速排序思想,通过分割找到中位数;另一种采用阵地攻守策略,遍历数组确定可能的主元素,并验证其是否符合条件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package Nowcode;

/**
 * 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。
 * 
 * @author Administrator 如果把这个数组排序那么排序之后数组中间的那个数一定就是那个出现次数超过数组长度一半的数字
 *
 */
public class MoreThanHalfNum {
	// 解法一o(n):基于快速排序的分割思想:在随机快速排序算法中,我们先在数组中随机选择一个数字,然后调整数组中数字的顺序,使得比选中的数字小的数字排在它的左边,比选中
	// 的数字大的排在它的右边,如果选中的数字的下标刚好是n/2,那么这个数字就是数组的中位数
	public int MoreThanHalfNum_Solution(int[] array) {
		if (array == null)
			return 0;
		int length = array.length;
		int start = 0;
		int end = length - 1;
		int middle = length >> 2;
		int index = Partition(array, length, start, end);
		// 如果选中的数字的下标刚好是n/2,那么这个数字就是数组的中位数
		while (index != middle) {
			// 如果选中的数字的下标刚好是n/2右边,调整左边数字
			if (index > middle) {
				end = index - 1;
				index = Partition(array, length, start, end);
			} else {
				// 如果选中的数字的下标刚好是n/2左边,调整右边数字
				start = index + 1;
				index = Partition(array, length, start, end);
			}
		}
		// 最后验证这个数字是不是真的超过数组的一半
		int count = 0;
		for (int i = 0; i < array.length; i++) {
			if (array[i] == array[index])
				count++;
		}
		if (count * 2 > array.length)
			return array[index];
		else
			return 0;
	}

	// 在数组中选择一个数,比选择的数字小的数字移到数组的左边,比选择数字大的移动到数组的右边
	int Partition(int[] data, int length, int start, int end) {
		if (data.length == 0 || length <= 0 || start < 0 || end >= length) {

			return -1;
		}
		// 选择从最后一个数为基准元素开始一次排序
		int index = start - 1;
		for (int i = start; i < end; ++i) {
			if (data[i] < data[end]) // 以最后一个元素为基准点进行划分
			{
				++index;
				if (index != i) {
					// 交换
					int temp = data[i];
					data[i] = data[index];
					data[index] = temp;
				}
			}
		}

		++index;
		// 交换
		int temp = data[index];
		data[index] = data[end];
		data[end] = temp;
		// System.out.print("index=" + index + " ");
		return index; // 返回一次排序后开始选择的数此刻所在最终位置索引

	}

	public static void main(String[] args) {
		int[] array = { 4, 2, 4, 4, 2, 4 };
		moreHalfNum m = new moreHalfNum();
		System.out.println(m.MoreThanHalfNum_Solution(array));

	}

}


package Nowcode;

/**
 * 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0
 * 
 * @author Administrator
 *
 */
public class moreHalfNum {
	// 方法二O(n):采用阵地攻守的思想
	public int MoreThanHalfNum_Solution(int[] array) {
		if (array == null)
			return 0;
		// 第一个数字作为第一个士兵,守阵地;count = 1;
		int result = array[0];
		int count = 1;

		for (int i = 1; i < array.length; i++) {
			// 当遇到count为0的情况,又以新的i值作为守阵地的士兵,继续下去
			if (count == 0) {
				result = array[i];
				count = 1;
				// 遇到相同元素,count++;
			} else if (array[i] == result)
				count++;
			// 遇到不相同元素,即为敌人,同归于尽,count--;
			else
				count--;

		}

		// 到最后还留在阵地上的士兵,有可能是主元素
		count = 0;
		for (int i = 0; i < array.length; i++) {
			if (array[i] == result)
				count++;
		}
		// 验证最后活下来的数字个数是不是超过数组的一半
		if (count * 2 > array.length)
			return result;
		else
			return 0;
	}

	public static void main(String[] args) {
		// int [] array={1,2,3,2,2,2,5,4,2};
		// int [] array={1,2,3,2,4,2,5,2,3};
		int[] array = { 4, 2, 1, 4, 2, 4 };
		moreHalfNum m = new moreHalfNum();
		System.out.println(m.MoreThanHalfNum_Solution(array));

	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值