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