Problem overview
The method swapFirstAndLast receives an ArrayBuffer[Int] named nums, swaps its first and last numbers, and returns it. The method changes nums itself and then returns that same buffer. The numbers in between stay where they are. [1, 2, 3, 4, 5] becomes [5, 2, 3, 4, 1], and [4, 9] becomes [9, 4]. With one number, that number is both the first and the last, so [7] stays [7].
Approach
An ArrayBuffer is a mutable, growable sequence from scala.collection.mutable. Items are read with nums(i) and written with nums(i) = value, and nums.size is the number of items. Because the buffer can be modified in place, the swap can be done on nums directly, with no new collection.
The trap in a swap is that two values can't trade places in one move. If you write the last number into the first place right away, the old first number is overwritten and lost. The standard fix is a temporary variable: save the first number somewhere safe, overwrite its place, and then put the saved number into the last place. It is like using a third cup to swap the drinks in two cups.
Step by step
- Save the first number in a
valnamedtemp:val temp = nums(0). - Copy the last number into the first place. The last index is
nums.size - 1, so the line isnums(0) = nums(nums.size - 1). - Put the saved number in the last place:
nums(nums.size - 1) = temp. - Return
nums. It is the same buffer that was passed in, now swapped.
The solution in Scala
class Solution {
def swapFirstAndLast(nums: ArrayBuffer[Int]): ArrayBuffer[Int] = {
val temp = nums(0)
nums(0) = nums(nums.size - 1)
nums(nums.size - 1) = temp
return nums
}
}
A few Scala details:
nums(0) = xis how Scala writes into a mutable collection. It is a call to the buffer'supdatemethod, written like an assignment.- The method modifies its argument. After
val result = solution.swapFirstAndLast(buffer), the namesresultandbufferrefer to the same object, sobufferis swapped too. - The type
ArrayBufferlives inscala.collection.mutable. In a file of your own, addimport scala.collection.mutable.ArrayBufferto use it. val tempnever changes after it is set, sovalis the right choice overvar.
Tracing an example
Start with nums = [1, 2, 3, 4, 5]. After val temp = nums(0), temp is 1. After nums(0) = nums(nums.size - 1), the buffer is [5, 2, 3, 4, 5]: the first place holds the old last number, and the 1 survives only in temp. After nums(nums.size - 1) = temp, the buffer is [5, 2, 3, 4, 1].
Skipping temp and writing nums(0) = nums(4) followed by nums(4) = nums(0) ends with [5, 2, 3, 4, 5]: the second line copies the 5 back, because the 1 is already gone.
Edge cases and common mistakes
- One number. The first and last index are both 0. The method saves the number, writes it to the same place and writes it back, so
[7]stays[7]with no special case. - Two numbers. The first and last places are neighbours, and the same three lines swap them:
[4, 9]becomes[9, 4]. - Using
nums.sizeas the last index. The last valid index isnums.size - 1. Indexnums.sizethrows an exception. - An empty buffer.
nums(0)would throw an exception. The lesson guarantees at least one number.
Testing
[1, 2, 3, 4, 5]gives[5, 2, 3, 4, 1].[4, 9]gives[9, 4].[7]gives[7].
