[220208] Add Digits
Given an integer
num
, repeatedly add all its digits until the result has only one digit, and return it.
class Solution:def addDigits(self, num):while len(str(num)) > 1:res = 0length = len(str(num))# 每一位数加上去for i in range(length):res += int(str(num)[i])# 给 num 重新赋值num = resreturn num
class Solution:def addDigits(self, num):# 特例:0 --> 0if num == 0:return 0# 能被 9 整除的数 --> 9elif num % 9 == 0:return 9# 其余数字 --> 找最近一个能被 9 整除的数,计算差值 (mod 9)else:return num % 9
[220208] Add Digits相关推荐
- Leetcode刷题记录[python]——258 Add Digits
一.前言 做这题有个小收获,关于Digital root的解法,有个极方便的小公式: 二.题258 Add Digits Given a non-negative integer num, repea ...
- leetcode Add Digits
题目连接 https://leetcode.com/problems/add-digits/ Add Digits Description Given a non-negative integer n ...
- [LeetCode] Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- [leetcode][math] Add Digits
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
- [LeetCode] Add Digits - 数字各个位数求和
题目概述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...
- Leet Code OJ 258. Add Digits [Difficulty: Easy]
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
- LeetCode之Add Digits
1.题目 Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...
- LeetCode 258. Add Digits
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
- C#LeetCode刷题之#258-各位相加(Add Digits)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3860 访问. 给定一个非负整数 num,反复将各个位上的数字相加 ...
最新文章
- “饶毅举报”事件尘埃落定,裴钢表示未发现裴钢造假。网友:我有信心一年发20篇SCI...
- Quora上的大牛们最喜欢哪种机器学习算法?
- Google Guava Collections 使用介绍
- python爬虫解决网页重定向问题
- access 导入 txt sql语句_从零开始学习 MySQL 系列索引、视图、导入和导出
- [深度学习] 自然语言处理 --- Self-Attention(三) 知识点与源码解析
- html5外置样式表,HTML5移动端通用css详解
- 少儿图论:八岁小孩眼里的欧拉公式
- linux java amr转mp3_部署ffmpeg及amr转mp3方法
- php70w mysql.x8664_LAMP环境的搭建(三)----PHP7的安装
- Oracle把Java EE的未来押在Rest API上了?
- html登陆滑动验证,js实现滑动滑块验证登录的方法
- Java中的IO整理
- 真实渗透改编--综合渗透 sql注入写马+文件上传+udf提权 安鸾靶场SQL注入学习
- 案例 | 巴别鸟为弘睿构建企业知识库
- 【积跬步以至千里】Markdownpad2报错: Html Rendering Error:An error occurred with the HTML rendering component。
- 最新电脑CPU天梯图
- tftp 在嵌入式设备和主机之间传输文件
- ‘findstr‘ 不是内部或外部命令,也不是可运行的程序或批处理文件...
- 数学建模Word排版——样式
热门文章
- 计算机应用基础选择题综合十,计算机应用基础选择题综合复习练习题
- android adb移植到arm,android-ndk – 为arm处理器构建android adb
- ibm服务器安装系统后黑屏,i52430m装win10系统重启电脑进入会黑屏修复方法
- 订阅者java,RxJava:“ java.lang.IllegalStateException:只允许一个订阅者!”
- 宁波python学习_python学习第二十天
- HTML 内容不能被选择,不能被复制
- SpringBoot整合kafka案例
- ORACLE查看某个表空间里有哪些表
- C++ BSS段、数据段、代码段、堆与栈及五大内存分区
- C语言中字符串的处理方式