[LeetCode]Add Digits
来源:互联网 发布:ai软件卡通图片 编辑:程序博客网 时间:2024/06/06 00:29
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:
- A naive implementation of the above process is trivial. Could you come up with other methods?
- What are all the possible results?
- How do they occur, periodically or randomly?
- You may find this Wikipedia article useful.
The formula is:
or,
class Solution {public: int addDigits(int num) { return 1+(num-1)%9; }};
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
- HashMap源码剖析
- 《构建之法》读后感
- codeforces_612D. The Union of k-Segments(排序)
- Java HashMap源代码详解
- ViewPager的无限轮播
- [LeetCode]Add Digits
- 快速排序
- codeforces 631B Print Check
- HTML空格占位符
- 组合数取模
- Java中的异常链
- 排列组合
- 关于安装wamp server之后没办法访问localhost和127.0.0.1
- No_16_0324 Java基础学习第二十三天