编程之美2.13子数组的最大乘积Java版

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Test;

/**
 *
 * 2.13 子数组的最大乘积
 */
public class SearchArrayMaxMultiply {

    public static void main(String[] args) {
        int[] arry = new int[]{-2, -1, -8, -5, -7, -12};
        //解法一
        int num1 = search_max_multiply1(arry);
        System.out.println("最大乘积=" + num1);
        //解法二
        int num2 = search_max_multiply2(arry);
        System.out.println("最大乘积=" + num2);
    }

    private static int search_max_multiply1(int[] arry) {
        int[] before = new int[arry.length];
        int[] after = new int[arry.length];

        int num = arry.length - 1;

        before[0] = 1;
        for (int i = 1; i < arry.length; i++) {
            before[i] = before[i - 1] * arry[i - 1];
        }
        after[num] = 1;
        for (int i = num - 1; i >= 0; i--) {
            after[i] = after[i + 1] * arry[i + 1];
        }
        int max = before[0] * after[0];
        for (int i = 1; i <= num; i++) {
            int tmp = before[i] * after[i];
            if (tmp > max) {
                max = tmp;
            }
        }

        return max;
    }

    private static int search_max_multiply2(int[] arry) {
        int max = 1;
        int p_num = 0;
        int n_num = 0;
        int z_num = 0;
        int min_p = 0;//最小正数
        int min_n = 0;//最小负数
        int max_n = 0;//最大负数
        for (int i = 0; i < arry.length; i++) {
            if (arry[i] > 0) {
                if (arry[i] < min_p || min_p == 0) {
                    min_p = arry[i];
                }
                p_num++;
            } else if (arry[i] < 0) {
                if (arry[i] < min_n) {
                    min_n = arry[i];
                }
                if (arry[i] > max_n || max_n == 0) {
                    max_n = arry[i];
                }
                n_num++;
            } else {
                z_num++;
            }
        }
        if (z_num >= 2) {
            max = 0;
            return max;
        } else {
            if (z_num == 1) {//只有一个0
                //去掉0的乘积为正数,即有偶数个负数,最大乘积是去掉0后的子数组的乘积
                if (n_num % 2 == 0) {
                    for (int i : arry) {
                        if (i != 0) {
                            max *= i;
                        }
                    }
                } else {//去掉0的乘积为负数,即有奇数个负数,最大乘积是0
                    max = 0;
                }
            } else {//如果没有0
                if (n_num % 2 == 0) {//如果完整数组的乘积是个正数,即有偶数个负数
                    if (p_num > 0) {//如果存在正数,最大乘积是去掉最小正数后的子数组的乘积
                        for (int i : arry) {
                            if (i != min_p) {
                                max *= i;
                            }
                        }
                    } else {//如果不存在正数,最大乘积是去掉最小负数后的子数组的乘积
                        for (int i : arry) {
                            if (i != min_n) {
                                max *= i;
                            }
                        }
                    }
                } else {//如果完整数组的乘积是个负数,即有奇数个负数。则最大乘积是去掉最大负数后的子数组的乘积
                    for (int i : arry) {
                        if (i != max_n) {
                            max *= i;
                        }
                    }
                }
            }
        }
        return max;
    }
}

你可能感兴趣的:(编程之美笔记)