题目要求

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

题目分析及思路

给定一个非负整数,要求将它的各位数相加得到一个新数并对该新数重复进行这样的过程,直到最后的数只有一位。可以使用递归的方法,不断地将给定数的各位数相加,跳出递归的条件是该数只有一位。

python代码

class Solution:

def addDigits(self, num: int) -> int:

if num >= 0 and num <= 9:

return num

else:

num = num // 10 + num % 10

return self.addDigits(num)

LeetCode 258 Add Digits 解题报告的更多相关文章

  1. 【LeetCode】 258. Add Digits 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:减1模9 方法三:直接模9 日 ...

  2. LN : leetcode 258 Add Digits

    lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...

  3. Java [Leetcode 258]Add Digits

    题目描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...

  4. LeetCode 258 Add Digits(数字相加,数字根)

    翻译 给定一个非负整型数字,反复相加其全部的数字直到最后的结果仅仅有一位数. 比如: 给定sum = 38,这个过程就像是:3 + 8 = 11.1 + 1 = 2.由于2仅仅有一位数.所以返回它. ...

  5. [LeetCode] 258. Add Digits 加数字

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

  6. LeetCode 258. Add Digits

    Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...

  7. (easy)LeetCode 258.Add Digits

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

  8. leetcode 258. Add Digits(数论)

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

  9. LeetCode: 258 Add Digits(easy)

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

随机推荐

  1. HeroKu PaaS模式

    从HeroKu的官网看到一副流程图,演示了其提倡的应用构建通道,让包括个人开发者.创业团队.乃至各种规模的业务都能以自己的方式使用它,剩下的就是交给用户去开发出优秀的应用. 在开发者和应用的用户之间, ...

  2. 块级格式化上下文( Block formatting contexts)

    那么如何触发BFC呢? float 除了none以外的值 overflow 除了visible 以外的值(hidden,auto,scroll ) display (table-cell,table- ...

  3. SmileyCount.java笑脸加法程序代写(QQ:928900200)

    SmileyCount.java 1/4Java Programming 2014Course Code: EBU4201Mini ProjectTask 1 [30 marks]SmileyCoun ...

  4. Why you should use async tasks in .NET 4.5 and Entity Framework 6

    Improve response times and handle more users with parallel processing Building a web application usi ...

  5. 关于CLOS架构的举例 网络级 设备级 FATTREE网络 网络级CLOS 以及CLOS涉及的调度算法RR

    1.概述 CLOS来自于传统电路交换概念,这个概念年代太久远,在当前数据通信网络中,内涵有所变化.本文主要谈的是实际上赋予的与原来略微有所差异的内涵. CLOS架构本身概念比较宽泛,有设备级的CLOS ...

  6. windows 注册表讲解

    注册表存储结构: 整个注册表内容主要由项(键).值(键值)构成.(通过regedit命令打开注册表) 5个根键: HKEY_CLASSES_ROOT    (缩写HKCR) HKEY_CURRENT_ ...

  7. Spark学习笔记——手写数字识别

    import org.apache.spark.ml.classification.RandomForestClassifier import org.apache.spark.ml.regressi ...

  8. 如何在django里面添加自定义命令

    第一步:创建对应的目录 第二步:继承父类,写自己的逻辑代码 第三步:执行 manage.py 查看自己的命令

  9. [DL] *Deep Learning for Industry - Wang Yi

    Link: 分布式机器学习系列讲座 - 04 Deep Learning WANG Yi. https://cxwangyi.wordpress.com/ https://www.zhihu.com/ ...

  10. ElasticSearch6(三)-- Java API实现简单的增删改查

    基于ElasticSearch6.2.4, Java API创建索引.查询.修改.删除,pom依赖和获取es连接 可查看此文章. package com.xsjt.learn; import java ...