public static void getGroup(int sum, int arr[]){
if(arr.length<=0) {
System.out.println("数组为空");
return;
}
int start = 0;
int end =arr.length-1;
while (start <end){
if(arr[start]+arr[end]== sum){
System.out.println("符合要求"+arr[start]+","+arr[end]);
start++;
end--;
}else if(arr[start]+arr[end] > sum) {
end--;
}else if(arr[start]+arr[end] < sum){
start++;
}
}
}
public static void main(String[] args) {
int[] arr ={-4,2,5,6,8,9,9,10,11,19};
getGroup(15,arr);
}
时间: 2024-10-09 03:34:00