1、单链表实现
public class LinkedList {
private final Integer[] week;
public LinkedList() {
week = new Integer[]{1, 2, 3, 4, 5, 6, 7};
}
public static Integer[] orderByWeekDayWithLinkedList(Integer[] week, int day) {
if (day < 1 || day > 7) throw new IllegalArgumentException();
if (1 == day) return week;
java.util.LinkedList<Integer> linkedList = new java.util.LinkedList(Arrays.asList(week));
java.util.LinkedList<Integer> restructureList = new java.util.LinkedList();
final int seperate_index = day - 1;
final int last_index = week.length;
List<Integer> lastList = linkedList.subList(0, day - 1);
List<Integer> headList = linkedList.subList(seperate_index, last_index);
restructureList.addAll(headList);
restructureList.addAll(lastList);
for (int i = 0; i < restructureList.size(); i++) {
week[i] = restructureList.get(i);
}
return week;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(orderByWeekDayWithLinkedList(new LinkedList().week, 1)));
System.out.println(Arrays.toString(orderByWeekDayWithLinkedList(new LinkedList().week, 2)));
System.out.println(Arrays.toString(orderByWeekDayWithLinkedList(new LinkedList().week, 3)));
System.out.println(Arrays.toString(orderByWeekDayWithLinkedList(new LinkedList().week, 4)));
System.out.println(Arrays.toString(orderByWeekDayWithLinkedList(new LinkedList().week, 5)));
System.out.println(Arrays.toString(orderByWeekDayWithLinkedList(new LinkedList().week, 6)));
System.out.println(Arrays.toString(orderByWeekDayWithLinkedList(new LinkedList().week, 7)));
}
}
2、求逆算法实现
public class Reverse {
private final Integer[] week;
public Reverse() {
week = new Integer[]{1, 2, 3, 4, 5, 6, 7};
}
public static Integer[] orderByWeekDayWithInverse(Integer[] week, int day) {
if (day < 0 || day > 7) throw new IllegalArgumentException();
if (day == 1) return week;
final int separate_index = day - 2;
final int last_index = week.length - 1;
final int reverse_index = last_index - (last_index - separate_index) + 1;
handle(week);
reverse(week, 0, separate_index);
reverse(week, reverse_index, last_index);
reverse(week, 0, last_index);
return week;
}
private static Integer[] handle(Integer[] i) {
if (null == i || 0 == i.length) return null;
return i;
}
private static Integer[] reverse(Integer[] arr, int from, int to) {
if (null == arr || 0 == arr.length
|| from < 0 || from > arr.length
|| to < 0 || to > arr.length)
throw new IllegalArgumentException();
int tmp;
while (from < to) {
tmp = arr[to];
arr[to] = arr[from];
arr[from] = tmp;
from++;
to--;
}
return arr;
}
public static void main(String[] args) {
Integer[] Monday = orderByWeekDayWithInverse(new Reverse().week, 1);
System.out.println(Arrays.toString(Monday));
Integer[] Tuesday = orderByWeekDayWithInverse(new Reverse().week, 2);
System.out.println(Arrays.toString(Tuesday));
Integer[] Wednesday = orderByWeekDayWithInverse(new Reverse().week, 3);
System.out.println(Arrays.toString(Wednesday));
Integer[] Thursday = orderByWeekDayWithInverse(new Reverse().week, 4);
System.out.println(Arrays.toString(Thursday));
Integer[] Friday = orderByWeekDayWithInverse(new Reverse().week, 5);
System.out.println(Arrays.toString(Friday));
Integer[] Saturday = orderByWeekDayWithInverse(new Reverse().week, 6);
System.out.println(Arrays.toString(Saturday));
Integer[] Sunday = orderByWeekDayWithInverse(new Reverse().week, 7);
System.out.println(Arrays.toString(Sunday));
}
}