[Leetcode] Add Digits
来源:互联网 发布:多用户商城源码下载 编辑:程序博客网 时间:2024/05/11 23:50
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?
public class Solution { public int addDigits(int num) { if(num<10) return num; int sum=0; while(num>0) { sum+=num%10; num=num/10; } return addDigits(sum); }}
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
- 欢迎使用CSDN-markdown编辑器
- 常见的linux的发行版及其联系和区别
- git gui工具介绍
- Java编程题目-8:元音字母调换位置
- bst
- [Leetcode] Add Digits
- Response对象应用(二)--生成随机验证码
- Android——Bitmap.decodeResource(Resources src, int id)和Bitmap.creatBitmap(...)
- 「iOS 100天学习计划」第八天~第十一天
- corosync+pacemaker测试用例
- 04-MapKit02-显示特定位置和区域
- POJ 2992 因子个数
- openjudge 海贼王之伟大航路(luoge.cpp/1000ms/128M)
- arduino双机通信 (解决引脚不够用)