LeetCode:Add Digits
来源:互联网 发布:苹果刷榜软件 编辑:程序博客网 时间:2024/05/13 11:59
问题描述:
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.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
Hint:
Show Hint
- A naive implementation of the above process is trivial. Could you come up with other methods?Show More Hint
- What are all the possible results?Show More Hint
- How do they occur, periodically or randomly?Show More Hint
- You may find this Wikipedia article useful.
思路:
digit = num - 9 * ((n - 1) / 9)
参考自维基百科:https://en.wikipedia.org/wiki/Digital_root
代码:
class Solution {public: int addDigits(int num) { int digit = num - 9 *((num - 1) / 9); return digit; }};
0 0
- [leetcode][math] Add Digits
- [leetcode] Add Digits
- 【leetcode】Add Digits【java】
- Leetcode: Add Digits
- LeetCode Add Digits
- [LeetCode] Add Digits
- leetcode 258: Add Digits
- LeetCode题解:Add Digits
- leetcode: Add Digits
- 258Add Digits - LeetCode
- leetCode #258 Add Digits
- [LeetCode 258] Add Digits
- LeetCode: Add Digits
- LeetCode:Add Digits
- leetcode--add digits
- leetcode Add Digits
- leetcode--Add Digits
- leetcode Add Digits
- HttpClient发送请求后得到的响应内容出现部分乱码的问题
- 《OpenGL编程指南》读书笔记2—第2章 着色器基础
- Action+Service +Dao三层的功能划分
- HDU - 2609 How many(最小表示法)
- lam 表达式比较
- LeetCode:Add Digits
- python中一个快速求素数的function
- 操作系统2-进程
- FZU - 1901 Period II(KMP next数组的理解)
- [Leetcode]Path Sum II
- Thinking in Java 整理笔记:内部类
- 学习java一个月的心得体会!!新手,不喜勿喷
- 【黑马程序员】成为一匹黑色战马,驰骋于技术的荒野之中
- IPA的4种打包方式