# 算法 – 找出数组中子集乘积的最大值

## 解法

// 计算数组中的所有字集的积
private List multiply(List result, int current, int... num) {
if (num.length == 0) return result;
if (num.length == 1) {
result.add(num[0]);
return result;
}
if (current == num.length - 1) return result;
int temp = 0;
for (int i = current; i < num.length - 1; i++) {
if (i == current) {
temp = num[i] * num[i + 1];
} else {
temp *= num[i + 1];
}
result.add(temp);
}
return multiply(result, current + 1, num);
}

// 找一个集合里最大的数
private Integer max(Integer init, int current, List num) {
if (num.size() == 0) return null;
if (num.size() < 2) return num.get(0);
if (init == null) {
return max(Math.max(num.get(0), num.get(1)), current + 2, num);
} else {
if (current == num.size() - 1) return init;
return max(Math.max(init, num.get(current)), current + 1, num);
}
}

@Test
public void test() {
int[] a = new int[]{2, 3, -2, 4};
List result = multiply(new ArrayList(), 0, a);
System.out.println("所有字集的积: " + result);
System.out.println(max(null, 0, result));
}

## 优解

@Test
public void test1() {
int[] nums = new int[]{2, 3, -2, 4};
int max = nums[0], min = nums[0], result = nums[0];
for (int i = 1; i  result) {
result = max;
}
}
System.out.println(result);
}