码迷,mamicode.com
首页 > 其他好文 > 详细

#Leetcode# 258. Add Digits

时间:2019-02-21 00:09:33      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:integer   until   xpl   out   cpp   col   input   its   blank   

https://leetcode.com/problems/add-digits/

 

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

Example:

Input: 38
Output: 2 
Explanation: The process is like: 3 + 8 = 11, 1 + 1 = 2. 
             Since 2 has only one digit, return it.

代码:

class Solution {
public:
    int addDigits(int num) {
        return (num == 0) ? 0 : (num - 1) % 9 + 1;
    }
};

  求数根 原来是有规律的 对 9 取余

FH

#Leetcode# 258. Add Digits

标签:integer   until   xpl   out   cpp   col   input   its   blank   

原文地址:https://www.cnblogs.com/zlrrrr/p/10409774.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!