LeetCode 258. 各位相加(Add Digits)
258. 各位相加
258. Add Digits
题目描述
给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。
LeetCode258. Add Digits
示例:
输出: 2
解释: 各位相加的过程为: 3 + 8 = 11, 1 + 1 = 2。 由于 2 是一位数,所以返回 2。
进阶:
你可以不使用循环或者递归,且在 O(1) 时间复杂度内解决这个问题吗?
Java 实现
class Solution {
public int addDigits(int num) {
return 1 + (num - 1) % 9;
}
}
相似题目
参考资料
LeetCode 258. 各位相加(Add Digits)的更多相关文章
- Java实现 LeetCode 258 各位相加
258. 各位相加 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由 ...
- [Swift]LeetCode258. 各位相加 | Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- Leetcode -- 258 数位相加
258. Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...
- Leetcode——258.各位相加【水题】
给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由于 2 是一位数,所 ...
- LeetCode(258.各位相加)的思路及解决过程
问题如下: 给一个非负整数 num,反复添加所有的数字,直到结果只有一个数字. 例如: 设定 num = 38,过程就像: 3 + 8 = 11, 1 + 1 = 2. 由于 2 只有1个数字,所以返 ...
- leetcode 258. 各位相加 (python)
给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由于 2 是一位数,所以 ...
- LeetCode:Add Digits - 非负整数各位相加
1.题目名称 Add Digits (非负整数各位相加) 2.题目地址 https://leetcode.com/problems/add-digits/ 3.题目内容 英文:Given a non- ...
- LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree
258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...
- LN : leetcode 258 Add Digits
lc 258 Add Digits lc 258 Add Digits Given a non-negative integer num, repeatedly add all its digits ...
随机推荐
- node.js Error: connect EMFILE 或者 getaddrinfo ENOTFOUND
Error: getaddrinfo ENOTFOUND] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo' Error: c ...
- socket.error: [Errno 9] Bad file descriptor
这个错误很明显 ,是因为你关闭了套接字对象后,又再次去调用了套接字对象,此时套接字链接已经被关闭,你不能再去调用,所以才会出现这种错误,复查一下自己的代码,很快就可以解决. 参考: https://b ...
- SignalR的几种方式_转自:https://www.cnblogs.com/zuowj/p/5674615.html
SignalR有三种传输模式: LongLooping(长轮询). WebSocket(HTML5的WEB套接字). Forever Frame(隐藏框架的长请求连接), 可以在WEB客户端显式指定一 ...
- 记录vsCode配置node开发环境
环境:win10,idea:vscode 1:安装Visual Studio Code 下一步下一步.(很早之前就安装了). 2:安装node. http://nodejs.cn/download/ ...
- ubuntu之路——day8.4 Adam自适应矩估计算法
基本上讲,Adam就是将day8.2提到的momentum动量梯度下降法和day8.3提到的RMSprop算法相结合的优化算法 首先初始化 SdW = 0 Sdb = 0 VdW = 0 Vdb = ...
- Spring Boot TImer Schedule Quartz
Spring Boot 2.X(十二):定时任务-云栖社区-阿里云https://yq.aliyun.com/articles/723876?spm=a2c4e.11155472.0.0.2f8b3a ...
- ThreadPoolExecutor 定制线程池参数
在 java 开发中经常需要执行一些“规格化”的任务,此时可以使用 java 线程池.节省创建线程时间,任务来时即可执行,高效. java 包是 java.util.concurrent .创建线程池 ...
- Kotlin数据类型 Unit、Nothing与Nothing?、Any与Any?
Kotlin数据类型 Unit.Nothing与Nothing?.Any与Any? 本文链接:https://blog.csdn.net/ldxlz224/article/details/9440 ...
- Zabbix3.4指定用户才能收到报警的配置
Zabbix3.4指定用户才能收到报警的配置 .下载python微信脚本 #安装simplejson wget https://pypi.python.org/packages/f0/07/26b51 ...
- IMDB-WIKI – 500k+ face images with age and gender labels论文学习
DEX: Deep EXpectation of apparent age from a single image 这个论文我们使用深度学习解决了在静态人脸图像中面部年龄的估计.我们的卷积神经网络使用 ...