ง่าย 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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก