Problem overview
The method swapFirstAndLast receives an array of integers, nums, swaps its first and last numbers and returns it. The array itself is changed, and the numbers between the two ends stay where they are. [1, 2, 3, 4, 5] becomes [5, 2, 3, 4, 1]. With two numbers, [4, 9] becomes [9, 4]. With one number, [7], the first and the last are the same item, so nothing changes.
Approach
Two values can't trade places in a single move. Suppose you first write the last number over the first place: the old first number is gone. The fix is a temporary variable that keeps the first number safe while the first place is overwritten, like a third cup when you swap the drinks in two cups.
The first number is at index 0 and the last one is at index nums.length - 1, because indexes start at 0.
Step by step
- Save the first number. Store
nums[0]in a constanttemp, declared withconstand the typenumber. The next step overwritesnums[0], so its old value must be saved now. - Copy the last number into the first place. Set
nums[0]tonums[nums.length - 1]. For[1, 2, 3, 4, 5]the array is now[5, 2, 3, 4, 5]. - Put the saved number in the last place. Set
nums[nums.length - 1]totemp. The array is now[5, 2, 3, 4, 1]. - Return
nums. The array itself was changed, so it comes back swapped.
The solution in TypeScript
class Solution {
swapFirstAndLast(nums: number[]): number[] {
const temp: number = nums[0];
nums[0] = nums[nums.length - 1];
nums[nums.length - 1] = temp;
return nums;
}
}
Some details of the code:
- Arrays are changed in place. The function receives a reference to the caller's array, not a copy. After
let arr = [1, 2, 3]and a call witharr, the variablearritself holds[3, 2, 1], and the returned value is the very same array: comparing the two with===givestrue. To keep the original, copy first, for example with[...nums]. constis enough fortemp. It is assigned once and never changes. The array's items are assigned, nottemp, and assigning an item is allowed even when the array was passed in as a parameter.- The item type is
number. The annotation ontempis erased when the code compiles, so it costs nothing at run time.
Tracing the example
With [1, 2, 3, 4, 5], temp is 1. After the second line the array is [5, 2, 3, 4, 5]. After the third it is [5, 2, 3, 4, 1]. With one number, [7], both indexes are 0: temp is 7, the array receives its own item, then temp, and stays [7].
Another way to swap
Destructuring swaps without a temporary variable: [nums[0], nums[nums.length - 1]] = [nums[nums.length - 1], nums[0]]; turns [1, 2, 3] into [3, 2, 1]. The lesson's version spells out each move, which shows why a temporary value is needed.
Edge cases and common mistakes
- Skipping the temporary variable. Writing
nums[0] = nums[nums.length - 1]and thennums[nums.length - 1] = nums[0]copies the last number twice. For[1, 2, 3]the result is[3, 2, 3]: the 1 is lost. - Using
nums.lengthas the last index. That index is past the end. The last index isnums.length - 1. - One-element arrays. No special case is needed. The first and the last index are both 0, and the swap leaves the array unchanged.
- Two-element arrays. The two items are next to each other and simply trade places:
[4, 9]becomes[9, 4].
Testing
[1, 2, 3, 4, 5]gives[5, 2, 3, 4, 1].[4, 9]gives[9, 4].[7]gives[7].
