题目要求

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. Git 藏匿操作

    假设您正在为您的产品实施的一项新功能.你的代码是在推进开发进度而客户不断升级需求突然来了.正因为如此,你必须保持放下你的新功能,工作几个小时.你不能提交你的部分代码,也不能扔掉你的变化.所以,你需要一 ...

  2. 运维监控系统之Open-Falcon

    一.Open-Falcon介绍 1.监控系统,可以从运营级别(基本配置即可),以及应用级别(二次开发,通过端口进行日志上报),对服务器.操作系统.中间件.应用进行全面的监控,及报警,对我们的系统正常运 ...

  3. shell中的函数 shell中的数组 告警系统需求分析

     

  4. C# Winform 防止MDI子窗体重复打开

    可以在MDI主窗体中添加以下方法. //防止打开多个窗体 private bool ShowChildrenForm(string p_ChildrenFormText) { int i; //依次检 ...

  5. excel中批量删除公式,保留数值

    excel中批量删除公式,保留数值 Sub macro1() Dim sh As Worksheet For Each sh In Sheets sh.UsedRange = sh.UsedRange ...

  6. 【DL】梯度下降小结

    温故知新 https://www.cnblogs.com/pinard/p/5970503.html

  7. 仿迅雷播放器教程 -- 基于ffmpeg的C++播放器 (1)

    2011年12月份的时候发了这篇博客 http://blog.csdn.net/qq316293804/article/details/7107049 ,博文最后说会开源一个播放器,没想到快两年了,才 ...

  8. 14桥接模式Bridge

    一.什么是桥接模式 Bridge 模式又叫做桥接模式,是构造型的设 计模式之一.Bridge模式基于类的最小设计原则,通过 使用封装,聚合以及继承等行为来让不同的类承担不同 的责任.它的主要特点是把抽 ...

  9. QT下载区

    http://download.qt.io/archive/qt/ 下载总网址: http://download.qt.io/ http://mirrors.hust.edu.cn/qtproject ...

  10. python网络编程之UDP方式传输数据

    UDP --- 用户数据报协议(User Datagram Protocol),是一个无连接的简单的面向数据报的运输层协议. UDP不提供可靠性,它只是把应用程序传给IP层的数据报发送出去,但是并不能 ...