Легко Решена: 1 Процент решений: 100.0%

Reverse a Number

Reverse the decimal digits of a positive integer.

Чему вы научитесь

  • Take digits off the end of a number one by one
  • Build a new number by shifting digits left (× 10)
  • Handle trailing zeros correctly

Условие

Показать перевод

Write the decimal digits of n in reverse order and return the resulting number. Zeros that end up at the front disappear, so 1200 becomes 21.

Implement reverse_number(n).

Input

n: A positive integer.

Output

Return the reversed number.

Ограничения

1 ≤ n ≤ 10¹⁸. The reversed value always fits a signed 64-bit integer.

Примеры

Пример 1
Ввод:
n = 352

Ожидаемый вывод:
expected = 253
Ввод:
long long n = 352;

Ожидаемый вывод:
long long expected = 253;
Ввод:
long n = 352;

Ожидаемый вывод:
long expected = 253;

Пояснение

The digits 3, 5, 2 reversed are 2, 5, 3.

Пример 2
Ввод:
n = 1200

Ожидаемый вывод:
expected = 21
Ввод:
long long n = 1200;

Ожидаемый вывод:
long long expected = 21;
Ввод:
long n = 1200;

Ожидаемый вывод:
long expected = 21;

Пояснение

Reversed digits are 0021; leading zeros are dropped, giving 21.

Как подступиться

Сначала попробуйте сами, затем открывайте подсказки по одной.

Подсказка 1

The key is to build the result one digit at a time from the end of the number.

Подсказка 2

Use repeated remainder and division to extract digits in reverse order.

Подсказка 3

Leading zeros in the reversed form should disappear naturally, so be careful not to preserve them.

Ожидаемая сложность

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

Лучшее принятое решение каждого участника. Более быстрые, экономные и высоко оценённые решения стоят выше.

# Участник Язык Время Память Оценка ИИ Решение
1 KKKaneki Ken Python 982 ms - 5/5 Посмотреть код

Учитесь у других

Решения сообщества

Внимание, спойлер: ниже 1 полное решение Попробуйте сначала решить сами. Вы всегда можете посмотреть код других и AI-обзоры. Войдите и решите задание, чтобы присоединиться к обсуждению.