LeetCode258. Add Digits 解题小结
Posted 医生工程师
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode258. Add Digits 解题小结相关的知识,希望对你有一定的参考价值。
题目:
Given a non-negative integer num
, repeatedly add all its digits until the result has only one digit.
For example:
Given num = 38
, the process is like: 3 + 8 = 11
, 1 + 1 = 2
. Since 2
has only one digit, return it.
参考这一篇wiki百科
https://en.wikipedia.org/wiki/Digital_root
class Solution { public: int addDigits(int num) { if (num >= 0 && num < 10) return num; return (num - 9 * ((num-1)/9)); } };
以上是关于LeetCode258. Add Digits 解题小结的主要内容,如果未能解决你的问题,请参考以下文章