258. Add Digits
O(1) soulution: 12345 % 9 = (1+2+3+4+5) % 9.
Based on a*10^k % 9 = a % 9.
Given a non-negative integer num
, repeatedly add all its digits until the result has only one digit.
Example:
1 | Input: 38 |
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
Soulution:
1 | class Solution: |