public static void main(String[] args) { int[] nums = {-3, -1, 0, 2, 4, 5}; int target = 0; ArrayList myAry = new ArrayList(); long adds = 0; Arrays.sort(nums); if (nums.length < 4) { // return myAry; System.out.println("g"); } int zuo = 0; int you = nums.length - 1; int x = zuo + 1; int y = you - 1; myAry.add(panduan(adds, zuo, y, x, you, nums, target, myAry)); System.out.println(myAry); } public static List<List<Integer>> panduan(long adds, int zuo, int y, int x, int you, int[] nums, int target, ArrayList myAry) { x = zuo + 1; y = you - 1; if(x==y){ return myAry; } while (true) { while (x <= y) { adds = nums[zuo] + nums[x] + nums[y] + nums[you]; if (adds == target) { if (myAry.size() == 0) { myAry.add(Arrays.asList(nums[zuo], nums[x], nums[y], nums[you])); } else { if (!myAry.contains(Arrays.asList(nums[zuo], nums[x], nums[y], nums[you]))) { myAry.add(Arrays.asList(nums[zuo], nums[x], nums[y], nums[you])); } else { break; } } x += 1; } else if (adds > target) { y -= 1; } else { x += 1; } } x = zuo + 1; y = you - 1; if (nums[zuo] + nums[x] + nums[y] + nums[you] < target) { panduan(adds, zuo+1, y, x, you, nums, target, myAry); } else if (nums[zuo] + nums[x] + nums[y] + nums[you] > target) { panduan(adds, zuo, y, x, you-1, nums, target, myAry); } else { myAry.add(panduan(adds, zuo, y, x, you-1, nums, target, myAry)); myAry.add(panduan(adds, zuo+1, y, x, you, nums, target, myAry)); } if (zuo + 1 == you - 1) { break; } } return myAry; }有什么问题
时间: 2024-04-16 20:28:10 浏览: 67
import java.io.*; public class FirstPart{ public static void main(String[] args) throws Exception{ System.out.print("The input Fahrenheit is "); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int f = Integer.parseInt(br.re
5星 · 资源好评率100%
这段代码是一个寻找四个数之和等于目标值的算法。它使用了递归的方法来遍历数组,找到符合条件的四个数,并将其添加到一个ArrayList中。最后输出这个ArrayList。
代码中存在一个问题:在递归调用panduan方法时,没有对返回值进行处理,导致无法正确地将结果添加到myAry中。需要将递归调用的结果添加到myAry中,可以使用addAll方法。
修改后的代码如下:
```java
public static void main(String[] args) {
int[] nums = {-3, -1, 0, 2, 4, 5};
int target = 0;
ArrayList<List<Integer>> myAry = new ArrayList<>();
long adds = 0;
Arrays.sort(nums);
if (nums.length < 4) {
System.out.println("g");
}
int zuo = 0;
int you = nums.length - 1;
int x = zuo + 1;
int y = you - 1;
myAry.addAll(panduan(adds, zuo, y, x, you, nums, target));
System.out.println(myAry);
}
public static List<List<Integer>> panduan(long adds, int zuo, int y, int x, int you, int[] nums, int target) {
x = zuo + 1;
y = you - 1;
List<List<Integer>> myAry = new ArrayList<>();
if(x == y){
return myAry;
}
while (true) {
while (x <= y) {
adds = nums[zuo] + nums[x] + nums[y] + nums[you];
if (adds == target) {
if (myAry.size() == 0) {
myAry.add(Arrays.asList(nums[zuo], nums[x], nums[y], nums[you]));
} else {
if (!myAry.contains(Arrays.asList(nums[zuo], nums[x], nums[y], nums[you]))) {
myAry.add(Arrays.asList(nums[zuo], nums[x], nums[y], nums[you]));
}
}
x += 1;
} else if (adds > target) {
y -= 1;
} else {
x += 1;
}
}
x = zuo + 1;
y = you - 1;
if (nums[zuo] + nums[x] + nums[y] + nums[you] < target) {
myAry.addAll(panduan(adds, zuo+1, y, x, you, nums, target));
} else if (nums[zuo] + nums[x] + nums[y] + nums[you] > target) {
myAry.addAll(panduan(adds, zuo, y, x, you-1, nums, target));
} else {
myAry.addAll(panduan(adds, zuo, y, x, you-1, nums, target));
myAry.addAll(panduan(adds, zuo+1, y, x, you, nums, target));
}
if (zuo + 1 == you - 1) {
break;
}
}
return myAry;
}
```
这样修改后,代码能够正确地输出符合条件的四个数的组合。
阅读全文