LeetCode No.258 Add Digits
来源:互联网 发布:网络平台借贷违法的吗 编辑:程序博客网 时间:2024/06/01 08:07
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.
===================================================================
题目链接:https://leetcode.com/problems/add-digits/
题目大意:计算num各个位的数字之和,直到最终结果是一位数。
思路:递归计算。
参考代码:
class Solution {public: int addDigits(int num) { if ( num < 10 ) return num ; int ans = 0 ; while ( num ) { ans += ( num % 10 ) ; num /= 10 ; } return addDigits ( ans ) ; }};
0 0
- LeetCode No.258 Add Digits
- leetcode NO 258. Add Digits
- leetcode 258: Add Digits
- 258Add 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
- leetcode 258 :Add Digits
- leetcode 258--Add Digits
- leetcode 258 Add Digits
- Leetcode #258 Add Digits
- java集合14--Map总结
- ③Cookie窃取攻击
- rsa加密算法
- [LeetCode]Majority Element
- C#语言和数据库技术基础 MyBank
- LeetCode No.258 Add Digits
- javascript中的深拷贝和浅拷贝?
- [noip2013tg] 火柴排队
- java集合15--set架构
- predis如何实现phpredis的pconnect方法
- 第11周 oj (FINAL)趣味取小数
- 素数判定、埃氏筛法与整数分解
- 洛谷 P2853 [USACO06DEC]牛的野餐Cow Picnic
- 缺项级数的收敛域求解