Leetcode 258. Add Digits
来源:互联网 发布:数据库关系代数 编辑:程序博客网 时间:2024/05/22 10:26
题目:
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?
思路:
- 用循环来想是比较简单的,但是对于复杂度有要求,那就一定有规律。
- 第一个开始计算的两位数就是10,从10开始逐次加一,9个一循环,就可以想出 num_return = (num-10)%9+1=(num-1)%9+1。
- return 1+(num)%9
0 0
- 【LeetCode】258.Add Digits
- LeetCode 258. Add Digits
- LeetCode : 258. Add Digits
- leetCode 258. Add Digits
- LeetCode 258. Add Digits
- 258. Add 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
- 258.[LeetCode]Add Digits
- 【leetcode】258. Add Digits
- 严重: Failed to destroy end point associated with ProtocolHandler ["http-nio-8080"] java.lang.NullPoin
- C++继承详解之三——菱形继承+虚继承内存对象模型详解vbptr(1)
- 二分图匹配总结
- ArcGIS教程:对地理处理服务和任务编写文档
- Android Studio超详细配置设置说明(18p图)
- Leetcode 258. Add Digits
- Myeclipse 卸载提示could not open jvm.cfg错误无法卸载
- Apache Thrift - 可伸缩的跨语言服务开发框架
- C#界面控件DotNetBar使用详解
- SQLite 存储模型(2)
- DELPHI 数据库心得
- 图片精灵处理
- 优化内存的一些小细节
- 前端开发面试题及答案