LeetCode:Add Digits
来源:互联网 发布:知乎 华西口腔正畸医生 编辑:程序博客网 时间:2024/05/02 01:55
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.
这道题很蛋疼,想了好久怎么能不用循环算,后来百度了下原来有公式。。怪我数学不怎么样咯。。
class Solution {public: int addDigits(int num) { return (num-1)%9+1; } };
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
- 用规则引擎来实现复杂业务逻辑判断之drools
- 测试笔记总结
- 如何对使用了autolayout的UIView添加动画
- Mac10.9.3仅安装Nginx
- HTML字符转码
- LeetCode:Add Digits
- JQuery中$.ajax()方法参数详解
- MFC截图工具(不断更新)
- libipvs.h:14:23: error: net/ip_vs.h: No such file or directory
- 一列变多列
- ImportNew 笔记
- C# 通过 Renci.SshNet 实现SFTP数据传输
- 开源评测系统hustoj-代码解读
- MSSQL 2008 链接服务器