簡単 0件解決済み 正答率0%

Smallest Digit

Find the smallest decimal digit of a positive integer.

このチャレンジで学べること

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

問題

翻訳を表示

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.

制約

1 ≤ n ≤ 10¹⁸

例

例 1
入力:
n = 352

期待される出力:
expected = 2
入力:
long long n = 352;

期待される出力:
long long expected = 2;
入力:
long n = 352;

期待される出力:
long expected = 2;

解説

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

例 2
入力:
n = 1203

期待される出力:
expected = 0
入力:
long long n = 1203;

期待される出力:
long long expected = 0;
入力:
long n = 1203;

期待される出力:
long expected = 0;

解説

The 0 inside the number is the smallest digit.

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

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

ヒント 2

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

ヒント 3

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

目安の計算量

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

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

まだ合格した解答はありません。解いて 1 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。