Einfach 0 gelöst 0% Erfolgsquote

Smallest Digit

Find the smallest decimal digit of a positive integer.

Das lernst du

  • Visit every digit of a number
  • Keep the smallest value seen so far
  • Choose a good starting value for a minimum

Problem

Übersetzung anzeigen

Look at every decimal digit of n and return the smallest one. Zeros inside the number count.

Implement smallest_digit(n).

Input

n: A positive integer.

Output

Return the smallest digit, from 0 to 9.

Einschränkungen

1 ≤ n ≤ 10¹⁸

Beispiele

Beispiel 1
Eingabe:
n = 352

Erwartete Ausgabe:
expected = 2
Eingabe:
long long n = 352;

Erwartete Ausgabe:
long long expected = 2;
Eingabe:
long n = 352;

Erwartete Ausgabe:
long expected = 2;

Erläuterung

The digits are 3, 5 and 2; the smallest is 2.

Beispiel 2
Eingabe:
n = 1203

Erwartete Ausgabe:
expected = 0
Eingabe:
long long n = 1203;

Erwartete Ausgabe:
long long expected = 0;
Eingabe:
long n = 1203;

Erwartete Ausgabe:
long expected = 0;

Erläuterung

The 0 inside the number is the smallest digit.

So gehst du vor

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

Hinweis 1

Compare every decimal digit and keep the smallest one seen so far.

Hinweis 2

Zeros matter here, so do not ignore them while scanning the digits.

Hinweis 3

Initialize carefully so the first digit always updates the running minimum.

Erwartete Komplexität

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

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.