Class Solution

java.lang.Object
g0201_0300.s0258_add_digits.Solution

public class Solution extends Object
258 - Add Digits.<p>Easy</p> <p>Given an integer <code>num</code>, repeatedly add all its digits until the result has only one digit, and return it.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> num = 38</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong></p> <pre><code> The process is 38 --> 3 + 8 --> 11 11 --> 1 + 1 --> 2 Since 2 has only one digit, return it. </code></pre> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> num = 0</p> <p><strong>Output:</strong> 0</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 <= num <= 2<sup>31</sup> - 1</code></li> </ul> <p><strong>Follow up:</strong> Could you do it without any loop/recursion in <code>O(1)</code> runtime?</p>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • addDigits

      public int addDigits(int num)