求一个不含0的数列(可以有正数和负数)的最大子序列乘积java

      //最大子序列乘积,序列中没有0,先判断有多少负数
	public static int maxSubMut(int[] array){
		if(array==null) return 0;
		if(array.length==1) return  array[0];
		int maxMut = 1;
		int maxLeft = 1;
		int maxRight = 1;
		int negative = 0;
		int k = 0;
		int[] negativeDiv = new int[array.length];
		for (int i = 0; i < array.length; i++) {
			if(array[i] < 0){
				negative++;
				negativeDiv[k] = i;
				k++;
			}
		}
		//有偶数个负数
		if(negative%2==0){
			for (int i = 0; i < array.length; i++) {
				maxMut *=array[i];
			}
		}
		else{
			int j = negativeDiv[k-1];
			for (int i = 0; i < j; i++) {
				maxLeft = maxLeft*array[i];
			}
			for (int i = j+1; i < array.length; i++) {
				maxRight = maxRight * array[i];
			}
			maxMut = Math.max(maxLeft, maxRight);
		}
		return maxMut;
}

你可能感兴趣的:(求一个不含0的数列(可以有正数和负数)的最大子序列乘积java)