Problem overview
The function swapFirstAndLast receives an array of integers, nums, swaps its first and last numbers and returns the result. The numbers in between stay where they are. When the array has only one number, that number is both first and last, so nothing changes.
For [1, 2, 3, 4, 5] the answer is [5, 2, 3, 4, 1]: 1 and 5 trade places while 2, 3 and 4 stay. For [4, 9] it is [9, 4]. For [7] it is [7].
Approach: a temporary variable, and a copy you can change
Two values can't trade places in a single move. If you copy the last number into the first place first, the old first number is gone. So one value has to wait somewhere: that is the job of a temporary variable, like a third cup when you swap the drinks in two cups.
Swift adds one more detail. The parameter nums is a constant inside the function, and an array is a value type, so the caller's array is never changed by the function. Assigning to an item of the parameter is a compile error:
error: cannot assign through subscript: 'nums' is a 'let' constant
The fix is to make a copy that you may change.
Step by step
- Make a changeable copy. Declare
var nums = nums. The new variable has the same name and the same numbers and hides the parameter, so from here onnumsmeans the copy. - Save the first number in a constant:
let temp = nums[0]. It waits there while the first place is overwritten. - Move the last number to the front with
nums[0] = nums[nums.count - 1]. Indexes start at 0, so the last one is the count minus 1. - Put the saved number last with
nums[nums.count - 1] = temp. - Return
nums, the swapped copy.
The solution in Swift
class Solution {
func swapFirstAndLast(_ nums: [Int]) -> [Int] {
var nums = nums
let temp = nums[0]
nums[0] = nums[nums.count - 1]
nums[nums.count - 1] = temp
return nums
}
}
Details worth knowing:
- Value semantics. Assigning an array to another variable gives an independent copy. After
var b = aandb[0] = 99, witha = [1, 2, 3],ais still[1, 2, 3]andbis[99, 2, 3]. That is why the function returns the result instead of changing the caller's array: calling it on[1, 2, 3, 4, 5]returns[5, 2, 3, 4, 1]and leaves the original at[1, 2, 3, 4, 5]. The copy is cheap, because Swift copies the storage only when one of the two arrays is changed. letversusvar.tempis set once, so it is alet. The copy changes, so it is avar.- One item. For
[7],nums.count - 1is 0, so the first and last places are the same. The code stores 7, writes 7 over it, writes 7 again, and returns[7]. - An empty array crashes.
nums[0]doesn't exist for an empty array, and the program stops at run time. The lesson always gives at least one number. - A built-in alternative.
swapAtswaps two positions of avararray:nums.swapAt(0, nums.count - 1)turns[1, 2, 3, 4, 5]into[5, 2, 3, 4, 1]. The lesson does the swap by hand to show how a temporary variable works.
Edge cases and common mistakes
- Skipping the temporary variable. Writing
nums[0] = nums[nums.count - 1]and thennums[nums.count - 1] = nums[0]sets both places to the old last number, because the first one was already overwritten. - Editing the parameter. Without
var nums = nums, the code doesn't compile. - Off by one.
nums[nums.count]is one past the end and crashes. The last index isnums.count - 1. - Two numbers.
[4, 9]gives[9, 4]: first and last are neighbours, and the same steps work. - Forgetting to return. The changes are made on a copy, so the function must return it.
Testing
[1, 2, 3, 4, 5]gives[5, 2, 3, 4, 1].[4, 9]gives[9, 4].[7]gives[7].[1, 2, 3]gives[3, 2, 1].[-5, 0, 5, 10]gives[10, 0, 5, -5].
