Leetcode-258题: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.

代码

class Solution(object):

    def addDigits(self, num):
        """
        :type num: int
        :rtype: int
        """
        while num > 9:
            t = num
            tot = 0
            while t != 0:
                tot += t%10
                t /= 10
            num = tot
        return num
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,779评论 0 33
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,959评论 0 23
  • 行为特质:虎型:大气、勇气、冒险、决断、担当;鹰型:缜密、布局、远虑、自制、不放弃;凤型:展现 专业能力: 沟通能...
    ZhangYY阅读 1,909评论 0 1
  • 对一些人而言,杂货就是杂货,对另一类人而言,杂货是一种生活。 譬如我。 从小,我就有一个梦想,开一间杂货铺,一间满...
    哎呀意阅读 312评论 3 2
  • 虽有缘,终是聚少离多早别离! 我先世是“戍籍”。所谓“戍籍”即是有罪充军至某地所记的户籍,被视为贱民之一。 我的父...
    问心浅笑阅读 870评论 6 9