問題
翻訳を表示Return a copy of the list with its first and last elements swapped. Lists with fewer than two elements stay the same.
Implement swap_first_last(items).
Input
items: The list of integers.
Output
Return the list with the ends swapped.
制約
0 ≤ length ≤ 100,000
例
例 1
入力:
items = [1, 2, 3, 4]
期待される出力:
expected = [4, 2, 3, 1]
入力:
std::vector<long long> items = {1, 2, 3, 4};
期待される出力:
std::vector<long long> expected = {4, 2, 3, 1};
入力:
long[] items = new long[] {1, 2, 3, 4};
期待される出力:
long[] expected = new long[] {4, 2, 3, 1};
解説
1 and 4 trade places: [4, 2, 3, 1].
例 2
入力:
items = [9]
期待される出力:
expected = [9]
入力:
std::vector<long long> items = {9};
期待される出力:
std::vector<long long> expected = {9};
入力:
long[] items = new long[] {9};
期待される出力:
long[] expected = new long[] {9};
解説
A single item has nothing to swap with: [9].
参考資料
この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。