#258

Add Digits

pupil · 330 · lc easy +22 · verified · 68.7% accepted · 5,469 likes · top 76%

play →

Description

Given a non-negative integer num, keep replacing it with the sum of its digits until only a single digit remains. Return that single digit.

Example 1:

Input: num = 38
Output: 2
Explanation: The process is
38 --> 3 + 8 --> 11
11 --> 1 + 1 --> 2
Since 2 has only one digit, return it.

Example 2:

Input: num = 0
Output: 0

Code

1
2
3