簡単 0件解決済み 正答率0%

Swap First and Last

Exchange the first and last items of a list.

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用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。