Đề bài
Xem bản dịchThe 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.
Ràng buộc
0 ≤ n ≤ 100,000
Ví dụ
Đầu vào:
numbers = [3, 0, 1]
Kết quả mong đợi:
expected = 2
Đầu vào:
std::vector<long long> numbers = {3, 0, 1};
Kết quả mong đợi:
long long expected = 2;
Đầu vào:
long[] numbers = new long[] {3, 0, 1};
Kết quả mong đợi:
long expected = 2;
Giải thích
n = 3, so the range is 0–3; 2 is missing.
Đầu vào:
numbers = [0]
Kết quả mong đợi:
expected = 1
Đầu vào:
std::vector<long long> numbers = {0};
Kết quả mong đợi:
long long expected = 1;
Đầu vào:
long[] numbers = new long[] {0};
Kết quả mong đợi:
long expected = 1;
Giải thích
n = 1 and the range is 0–1; 1 is missing.
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.