โจทย์
ดูฉบับแปลThe list holds n distinct numbers taken from 0, 1, …, n, so exactly one number in that range is missing. Find it. (Hint: compare the expected total with the actual total.)
Implement missing_number(numbers).
Input
numbers: n distinct numbers from 0 to n.
Output
Return the missing number.
เงื่อนไข
0 ≤ n ≤ 100,000
ตัวอย่าง
ตัวอย่างที่ 1
อินพุต:
numbers = [3, 0, 1]
ผลลัพธ์ที่คาดหวัง:
expected = 2
อินพุต:
std::vector<long long> numbers = {3, 0, 1};
ผลลัพธ์ที่คาดหวัง:
long long expected = 2;
อินพุต:
long[] numbers = new long[] {3, 0, 1};
ผลลัพธ์ที่คาดหวัง:
long expected = 2;
คำอธิบาย
n = 3, so the range is 0–3; 2 is missing.
ตัวอย่างที่ 2
อินพุต:
numbers = [0]
ผลลัพธ์ที่คาดหวัง:
expected = 1
อินพุต:
std::vector<long long> numbers = {0};
ผลลัพธ์ที่คาดหวัง:
long long expected = 1;
อินพุต:
long[] numbers = new long[] {0};
ผลลัพธ์ที่คาดหวัง:
long expected = 1;
คำอธิบาย
n = 1 and the range is 0–1; 1 is missing.
แหล่งอ้างอิง
โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน