Array · 1 of 36
Reverse the array
Turn a line of kids around so the last kid is first, by having kids from the two ends swap places until they meet in the middle.
- Simple way
- time O(n)
room O(n) - Clever way
- time O(n)
room O(1)
The problem, simply
You get an array. Reverse it: the last item becomes the first, the second to last becomes the second, and so on. The best answer changes the array itself instead of making a new one.
A story to remember it
Picture it
Seven items. Item 0 trades with item 6, item 1 with item 5, item 2 with item 4. The middle item 3 stays where it is.
- checking
- ignored
Examples
An odd count
- In
[1, 2, 3, 4, 5]- Out
[5, 4, 3, 2, 1]
Two swaps. The middle 3 stays.
An even count
- In
[4, 5, 1, 2]- Out
[2, 1, 5, 4]
Two swaps: 4 with 2, then 5 with 1. There is no middle item.
Letters
- In
['h', 'e', 'l', 'l', 'o']- Out
['o', 'l', 'l', 'e', 'h']
A word is a row of letters, so the same swaps reverse it.
One item
- In
[7]- Out
[7]
Nothing to swap.
Empty
- In
[]- Out
[]
Nothing to do, and the code must not crash.
Way 1: the simple way
Make a new array of the same size. Walk the old array from the back and write into the new one from the front.
- Make
outwithnslots. - For
ifrom 0 ton - 1, setout[i] = arr[n - 1 - i]. - Return
out.
class Solution {
public int[] reverseArray(int[] arr) {
int n = arr.length;
int[] out = new int[n];
for (int i = 0; i < n; i++) {
out[i] = arr[n - 1 - i]; // read from the back, write from the front
}
return out;
}
}How long it takes: one walk, O(n). Extra room: a whole new array, O(n).
Way 2: the clever way
Two fingers, one at each end.
lo = 0,hi = n - 1.- While
lo < hi: swaparr[lo]andarr[hi], then moveloright andhileft. - When they meet or cross, stop.
Swap the ends, step inward
01 / 05
Put lo on the first item and hi on the last item.
- looking now
- checking
- done
- answer
class Solution {
public void reverseArray(int[] arr) {
int lo = 0, hi = arr.length - 1;
while (lo < hi) {
int t = arr[lo]; // swap the two ends
arr[lo] = arr[hi];
arr[hi] = t;
lo++; // step inward
hi--;
}
}
}How long it takes: n / 2 swaps, which is still O(n). Extra room: two fingers and one temp, O(1).
Watch out for
- Use
lo < hi, notlo <= hi. Swapping the middle item with itself is harmless, but<=is a sign the stop rule is not clear in your head. - For an empty array,
histarts at -1, so the loop never runs. Good. - In Java,
int[]is changed in place, so the caller sees the reversed array. Returning a new array instead is a different contract; say which you do.
What interviewers ask next
- Do it with recursion.
reverse(lo, hi): iflo >= hireturn, else swap and callreverse(lo + 1, hi - 1). That usesO(n)room on the call stack. - Reverse only part of the array, from
ltor. Start the two fingers atlandr. This is the building block for rotating an array byk: reverse the whole thing, then reverse the two parts. - Reverse the words in a sentence. Reverse the whole string, then reverse each word.
- Reverse a linked list. There is no index to jump to the end, so you flip the arrows one node at a time instead.
Remember this
Grown-up words: the two fingers are two pointers, changing the array itself is in place, and a stretch of the array from l to r is a subarray.