LeetCode 258 Add Digits
来源:互联网 发布:mac下载软件在哪里下载 编辑:程序博客网 时间:2024/06/05 19:36
题目描述
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.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
分析
公式:
代码
public int addDigits(int num) { return 1 + (num - 1) % 9; }
1 0
- leetcode 258: Add Digits
- 258Add Digits - LeetCode
- leetCode #258 Add Digits
- [LeetCode 258] Add Digits
- LeetCode(258)Add Digits
- leetcode-258-Add Digits
- [leetcode 258]Add Digits
- [Leetcode]#258 Add Digits
- LeetCode 258----Add Digits
- LeetCode 258 : Add Digits
- 【LeetCode】258 Add Digits
- Leetcode - 258 - Add Digits
- leetcode 258 :Add Digits
- leetcode 258--Add Digits
- leetcode 258 Add Digits
- Leetcode #258 Add Digits
- LeetCode 258:Add Digits
- LeetCode 258:Add Digits
- 超详细mysql left join,right join,inner join用法分析
- Markdown编辑器写博客
- Ubuntu14.04LTS(64)编译安装lamp环境
- UI中的继承源图
- 编辑距离
- LeetCode 258 Add Digits
- Android之AppCompatActivity与Activity区别
- ERROR: Error in Log_event::read_log_event(): 'read error', data_len: 234781854, event_type: 2
- 浅谈Future与Callable
- python连接hiveserver2
- java中的类总结
- IOS 自定义字体
- Chrome 浏览器JSONView插件安装
- IOS框架和服务