Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Smallest Digit

Find the smallest decimal digit of a positive integer.

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

1 ≤ n ≤ 10¹⁸

Ví dụ

Ví dụ 1
Đầu vào:
n = 352

Kết quả mong đợi:
expected = 2
Đầu vào:
long long n = 352;

Kết quả mong đợi:
long long expected = 2;
Đầu vào:
long n = 352;

Kết quả mong đợi:
long expected = 2;

Giải thích

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

Ví dụ 2
Đầu vào:
n = 1203

Kết quả mong đợi:
expected = 0
Đầu vào:
long long n = 1203;

Kết quả mong đợi:
long long expected = 0;
Đầu vào:
long n = 1203;

Kết quả mong đợi:
long expected = 0;

Giải thích

The 0 inside the number is the smallest digit.

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

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

Gợi ý 2

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

Gợi ý 3

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

Độ phức tạp mong đợi

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

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

Chưa có lời giải nào đạt. Giải bài này để giành hạng nhất.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.