package t21;
import java.util.LinkedList;
//有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
//1 2 3 4 5-2-4 5 1 2 3
public class Test {
public static void main(String[] args) {
LinkedList<Integer> list=new LinkedList<Integer>();
for(int i=1;i<=5;i++) {
list.add(i);
}
System.out.println(exchange(list, 2));
}
public static LinkedList<Integer> exchange(LinkedList<Integer> list,int m) {
for(int i=0;i<m;i++) {
list.addFirst(list.removeLast());
}
return list;
}
// public static int[] exchange(int[] arr,int m) {
// int[] newArr=new int[arr.length];
// System.arraycopy(arr, 0, newArr, m, arr.length-m);
// System.arraycopy(arr, m+1, newArr, 0, m);
// return newArr;
// }
}
Java_有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
最新推荐文章于 2023-01-06 10:06:30 发布