Leet Code OJ 258. Add Digits [Difficulty: Easy]
生活随笔
收集整理的這篇文章主要介紹了
Leet Code OJ 258. Add Digits [Difficulty: Easy]
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目:
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?
代碼實現:
public class Solution {public int addDigits(int num) {if(num>9){return addDigits(num%10+addDigits(num/10));}else{return num;}} }總結
以上是生活随笔為你收集整理的Leet Code OJ 258. Add Digits [Difficulty: Easy]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leet Code OJ 231. Po
- 下一篇: Leet Code OJ 104. Ma