Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Swap First and Last

Exchange the first and last items of a list.

Đề bài

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.

Ràng buộc

0 ≤ length ≤ 100,000

Ví dụ

Ví dụ 1
Đầu vào:
items = [1, 2, 3, 4]

Kết quả mong đợi:
expected = [4, 2, 3, 1]
Đầu vào:
std::vector<long long> items = {1, 2, 3, 4};

Kết quả mong đợi:
std::vector<long long> expected = {4, 2, 3, 1};
Đầu vào:
long[] items = new long[] {1, 2, 3, 4};

Kết quả mong đợi:
long[] expected = new long[] {4, 2, 3, 1};

Giải thích

1 and 4 trade places: [4, 2, 3, 1].

Ví dụ 2
Đầu vào:
items = [9]

Kết quả mong đợi:
expected = [9]
Đầu vào:
std::vector<long long> items = {9};

Kết quả mong đợi:
std::vector<long long> expected = {9};
Đầu vào:
long[] items = new long[] {9};

Kết quả mong đợi:
long[] expected = new long[] {9};

Giải thích

A single item has nothing to swap with: [9].

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.