Einfach 0 gelöst 0% Erfolgsquote

First Invalid Age

Validate typed ages and report where the first bad entry is instead of letting the program crash.

Das lernst du

  • Check that text contains only digits
  • Validate a number against a range
  • Report the position of the first bad entry

Problem

Übersetzung anzeigen

A sign-up form receives ages as text. An entry is valid when, after trimming spaces at both ends, it consists only of digits (no sign) and its value is between 0 and 150 inclusive.

Find the first invalid entry so the form can point the user to it.

Implement first_invalid_age(values).

Input

values: The typed ages, in form order.

Output

Return the index of the first invalid entry, or -1 if all of them are valid.

Einschränkungen

0 ≤ number of entries ≤ 10,000

Each entry has at most 20 characters

Beispiele

Beispiel 1
Eingabe:
values = ["25", " 40 ", "abc", "200"]

Erwartete Ausgabe:
expected = 2
Eingabe:
std::vector<std::string> values = {"25", " 40 ", "abc", "200"};

Erwartete Ausgabe:
long long expected = 2;
Eingabe:
string[] values = new string[] {"25", " 40 ", "abc", "200"};

Erwartete Ausgabe:
long expected = 2;

Erläuterung

"25" and " 40 " (spaces trimmed) are valid; "abc" at index 2 is the first invalid entry.

Beispiel 2
Eingabe:
values = ["0", "150", "007"]

Erwartete Ausgabe:
expected = -1
Eingabe:
std::vector<std::string> values = {"0", "150", "007"};

Erwartete Ausgabe:
long long expected = -1;
Eingabe:
string[] values = new string[] {"0", "150", "007"};

Erwartete Ausgabe:
long expected = -1;

Erläuterung

All three are digits between 0 and 150 (007 is 7), so the answer is -1.

So gehst du vor

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

Hinweis 1

Trim the entry, then check it is non-empty and made only of digits.

Hinweis 2

Only after that, compare its value with 150.

Hinweis 3

A minus sign makes the entry invalid, and 007 is a valid 7.

Erwartete Komplexität

O(total length) time, O(1) extra space

Referenzen

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

Die beste bestandene Lösung jedes Coders. Schnellere, sparsamere und besser bewertete Lösungen stehen weiter oben.

Noch keine bestandene Lösung. Löse sie und hol dir Platz eins.

Von anderen lernen

Community-Lösungen

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