Add Digits
2017-04-06 16:34
162 查看
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.
For example:
Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
var addDigits = function(num) { var str = num.toString(), res = 0, tmp1, tmp2; for(var i = 0; i < str.length; i++){ res = parseInt(str[i]) + res; if(res >= 10){ tmp1 = parseInt(res / 10); tmp2 = res % 10; res = tmp1 + tmp2; } } return res; };
相关文章推荐
- LeetCode-258 Add Digits
- Add Digits
- 【LeetCode】 Add Digits 数字相加
- leetcode submission/20161013(add digits)
- [LeetCode]Add Digits
- 数据结构实例<四>(Add Digits)容易
- 258. Add Digits
- leetcode: Add Digits
- 258. Add Digits
- 258. Add Digits
- 258. Add Digits
- 【LeetCode】Add Digits
- leetcode解题报告258——Add Digits
- (leetcode)Add Digits
- LeetCode OJ 之 Add Digits (数字相加)
- LeetCode -- Add Digits
- LeetCode: Add Digits
- Add Digits
- 【LEETCODE】ADD DIGITS
- Add Digits