[leetcode][math] Add Digits
生活随笔
收集整理的這篇文章主要介紹了
[leetcode][math] 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.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
Hint:
class Solution { public:int addDigits(int num) {if(num == 0) return 0;if(num % 9 == 0) return 9;return num%9;} };
總結
以上是生活随笔為你收集整理的[leetcode][math] Add Digits的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 单人纸牌_NOI导刊2011提高(04)
- 下一篇: 12306 说:有时候,能排队也是一种幸