Add Digits
来源:互联网 发布:mysql技术精粹 pdf 编辑:程序博客网 时间:2024/05/21 06:16
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?
枚举1-20可发现所有的数字之间都是有规律的,以9为周期,不断循环
public class Solution { public int addDigits(int num) { if(num %9 !=0) return num%9; if(num==0) return 0; return 9; }}
0 0
- Add Digits
- Add Digits
- Add Digits
- Add Digits
- Add Digits
- Add Digits
- Add Digits
- Add Digits
- Add Digits
- Add Digits
- Add Digits
- Add Digits
- Add Digits
- Add Digits
- Add Digits
- Add Digits
- Add Digits
- Add Digits
- Clipping Views安卓裁剪工具的使用,使用Android5.0新技术绘制圆角矩形,圆形控件。
- led驱动
- QtXlsx使用方法(强大的Excel)
- Principle_of_Computing Assignment 1: 2048_Merge
- Leetcode_c++:Plus one(066)
- Add Digits
- CRC原理及其C语言、Java语言实现
- 汉字unicode编码表
- leetcode 322. Coin Change
- 二进制记录商品状态,超详细代码
- POJ 3329 TSP变形 / floyd预处理+状压DP
- Leetcode_c++:Set Matrix Zeros (073)
- 献给在外奋斗的人-也献给我自己
- [leetcode] 【链表】25. Reverse Nodes in k-Group