java实现给定一个数组和最大值最小值,找到一个数或哪几个数相加处于最大值最小值之间,如果没有,则需要找到拆分哪个数才能实现要求。
要求:给定的数组之和一定大于最大值。
static List<Integer> findCombinationHelper(List<Integer> list, int min, int max, int index, int currentSum, List<Integer> currentCombination) {
// 基本情况:当前和满足条件,添加当前组合到结果中
if (currentSum >= min && currentSum <= max) {
return new ArrayList<Integer>(currentCombination);
}
// 基本情况:当前和超出最大值,停止递归
if (currentSum > max) {
return new ArrayList<Integer>();
}
// 基本情况:到达列表末尾,没有找到组合
if (index == list.size()) {
return new ArrayList<Integer>();
}
// 递归情况:不选择当前元素
List<Integer> withoutCurrent = findCombinationHelper(list, min, max, index + 1, currentSum, currentCombination);
if (!withoutCurrent.isEmpty()) {
return withoutCurrent;
}
// 递归情况:选择当前元素
List<Integer> withCurrent = new ArrayList<Integer>(currentCombination);
withCurrent.add(list.get(index));
return findCombinationHelper(list, min, max, index + 1, currentSum + list.get(index), withCurrent);
}
public static int[] findCombination(List<Integer> list, int min, int max) {
// 主逻辑
List<Integer> resultList = new ArrayList<Integer>();
for (int i = 0; i < list.size(); i++) {
int num = list.get(i);
if (num >= min && num <= max) {
return new int[]{num}; // 找到单个满足条件的元素,直接返回
}
}
// 如果没有找到单个满足条件的元素,尝试组合
List<Integer> combinations = findCombinationHelper(list, min, max, 0, 0, new ArrayList<Integer>());
if (combinations.isEmpty()) {
return new int[]{}; // 找不到任何组合,返回空数组
}
// 将结果转换为数组
int[] result = new int[combinations.size()];
for (int i = 0; i < combinations.size(); i++) {
result[i] = combinations.get(i);
}
return result;
}
public static void main(String[] args) {
// 示例调用
List<Integer> list1 = Arrays.asList(1, 4, 5);
int[] result1 = findCombination(list1, 6, 8);
System.out.println(Arrays.toString(result1)); // 输出: [1, 5]
list1 = Arrays.asList(4, 4, 6);
result1 = findCombination(list1, 6, 8);
System.out.println(Arrays.toString(result1)); // 输出: [6]
List<Integer> list2 = Arrays.asList(1, 3, 5);
int[] result2 = findCombination(list2, 3, 8);
System.out.println(Arrays.toString(result2)); // 输出: [3]
List<Integer> list3 = Arrays.asList(1,3,5,13,13);
int[] result3 = findCombination(list3, 10, 12);
System.out.println(Arrays.toString(result3)); // 输出: []
if(result3.length==0){
System.out.println(Arrays.toString(splitStock(list3, 10, 12))); // 输出: [13, 1, 12]
}
list1 = Arrays.asList(9,8);
result1 = findCombination(list1, 10,10);
System.out.println(Arrays.toString(result1)); // 输出: []
if(result1.length==0){
System.out.println(Arrays.toString(splitStock(list1, 10, 10))); // 输出: [8, 7, 1]
}
list1 = Arrays.asList(2,2,2,2,3);
result1 = findCombination(list1, 10, 10);
System.out.println(Arrays.toString(result1)); // 输出: []
if(result1.length==0){
System.out.println(Arrays.toString(splitStock(list1, 10, 10))); // 输出: [2, 1, 1]
}
list1 = Arrays.asList(3,3,6);
result1 = findCombination(list1, 10, 10);
System.out.println(Arrays.toString(result1)); // 输出: []
if(result1.length==0){
System.out.println(Arrays.toString(splitStock(list1, 10, 10))); // 输出: [3, 2, 1]
}
list1 = Arrays.asList(3,3,6);
result1 = findCombination(list1, 0, 2);
System.out.println(Arrays.toString(result1)); // 输出: []
if(result1.length==0){
System.out.println(Arrays.toString(splitStock(list1, 0, 2))); // 输出: [6, 4, 2]
}
}
private static int[] splitStock(List<Integer> list, int min, int max) {
Collections.sort(list);
Integer maxNum = list.get(list.size() - 1);
if(maxNum>max){
return new int[]{maxNum,maxNum-max,max}; // 保留拆分出来的max,其他所有的打标识
}else {
for (int i = 0; i < list.size()-1; i++) {
// 到这里的都不会出现处于区间的,所以肯定只有可能大于max,然后拆分小的即可
if((maxNum+=list.get(i))>=max){
// 保留list.get(i)之前的元素和拆分出来的max-(maxNum-list.get(i)),list.get(i)之后的元素和剩余的maxNum-max则需要打标识
return new int[]{list.get(i),maxNum-max,max-(maxNum-list.get(i))};
}
}
}
return new int[]{};
}