Einfach 0 gelöst 0% Erfolgsquote

Missing Number

Find the one number missing from 0..n.

Das lernst du

  • Compute the expected sum of 0..n
  • Find the missing value from the difference
  • Solve it in one pass without sorting

Problem

Übersetzung anzeigen

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.

Einschränkungen

0 ≤ n ≤ 100,000

Beispiele

Beispiel 1
Eingabe:
numbers = [3, 0, 1]

Erwartete Ausgabe:
expected = 2
Eingabe:
std::vector<long long> numbers = {3, 0, 1};

Erwartete Ausgabe:
long long expected = 2;
Eingabe:
long[] numbers = new long[] {3, 0, 1};

Erwartete Ausgabe:
long expected = 2;

Erläuterung

n = 3, so the range is 0–3; 2 is missing.

Beispiel 2
Eingabe:
numbers = [0]

Erwartete Ausgabe:
expected = 1
Eingabe:
std::vector<long long> numbers = {0};

Erwartete Ausgabe:
long long expected = 1;
Eingabe:
long[] numbers = new long[] {0};

Erwartete Ausgabe:
long expected = 1;

Erläuterung

n = 1 and the range is 0–1; 1 is missing.

So gehst du vor

Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.

Hinweis 1

The numbers should match the full range from 0 through n except for one hole.

Hinweis 2

Compare what the complete range would total against what the list actually totals.

Hinweis 3

Watch for the case where the missing value is 0 or n.

Erwartete Komplexität

O(n) time, O(1) extra space

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.