继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

leetcode 258. Add Digits

Freya0_0
关注TA
已关注
手记 34
粉丝 71
获赞 639

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.

public class Solution {
    public int addDigits(int num) {
        if(num <10) { 
            return num; 
        }
        int t=0;
        while(num>=1){
           t+=num%10;
           num=num/10;
        }
        return addDigits(t);
     }
 }

http://www.cnblogs.com/wxisme/

打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP