Tutte le sfide
Facile 0 risolte 0% di accettazione

Largest Digit

Find the largest decimal digit of a positive integer.

Cosa imparerai

  • Visit every digit of a number
  • Keep the best value seen so far
  • Compare values inside a loop

Problema

Mostra traduzione

Look at every decimal digit of n and return the largest one.

Implement largest_digit(n).

Input

n: A positive integer.

Output

Return the largest digit, from 0 to 9.

Vincoli

1 ≤ n ≤ 10¹⁸

Esempi

Esempio 1
Input:
n = 352

Output atteso:
expected = 5
Input:
long long n = 352;

Output atteso:
long long expected = 5;
Input:
long n = 352;

Output atteso:
long expected = 5;

Spiegazione

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

Esempio 2
Input:
n = 7

Output atteso:
expected = 7
Input:
long long n = 7;

Output atteso:
long long expected = 7;
Input:
long n = 7;

Output atteso:
long expected = 7;

Spiegazione

The only digit is 7.

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

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

Suggerimento 2

Extract digits one by one from the right, or inspect the string characters.

Suggerimento 3

A number containing 9 anywhere immediately sets the answer to 9.

Complessità attesa

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

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

Ancora nessuna soluzione superata. Risolvila e prendi il primo posto.

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.