Non-Leetcode Questions: Four Integers
有4个正数,A,B,C,D。改变他们的位置使得他们两两之间的距离之和最大。
public int[] solution(int A, int B, int C, int D){
int num[] = {A,B,C,D};
Arrays.sort(num);
swap(num, 0, 2);
swap(num, 1, 3);
swap(num, 0, 3);
return num;
}
private void swap(int[] num, int x, int y){
int temp = num[x];
num[x] = num[y];
num[y] = temp;
}
Read full article from Non-Leetcode Questions: Four Integers
有4个正数,A,B,C,D。改变他们的位置使得他们两两之间的距离之和最大。
public int[] solution(int A, int B, int C, int D){
int num[] = {A,B,C,D};
Arrays.sort(num);
swap(num, 0, 2);
swap(num, 1, 3);
swap(num, 0, 3);
return num;
}
private void swap(int[] num, int x, int y){
int temp = num[x];
num[x] = num[y];
num[y] = temp;
}
Read full article from Non-Leetcode Questions: Four Integers