Make reverse a lot simpler

This commit is contained in:
Siwat Sirichai 2020-11-06 23:03:28 +07:00
parent b38f5ffc8e
commit c68ec4e11b
2 changed files with 1 additions and 10 deletions

View File

@ -20,18 +20,9 @@ public class RecursiveExercise {
return null;
if (ht.length == 1)
return ht.clone();
return append(new int[] { ht[ht.length - 1] }, append(
reverse(MyArrayUtil.tail(removeLast(ht.clone(), new int[ht.length - 1], 0))), new int[] { ht[0] }));
return append(reverse(MyArrayUtil.tail(ht)),new int[] {MyArrayUtil.head(ht)});
}
// Remove the last element
public static int[] removeLast(int[] ia1, int[] ia2, int i) {
ia2[i] = ia1[i];
if (i == ia2.length - 1)
return ia2;
return removeLast(ia1, ia2, i + 1);
}
public static boolean isIn(int x, int[] a) throws Exception {
// fill your code here