解题思路
当num<10直接返回,否则返回addDigits(num各位的和),用递归可以很轻松解决这道题,代码如下:
代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13class Solution { public: int addDigits(int num) { if(num < 10) return num; int temp = 0; string s = to_string(num); for(char c : s) { temp += c - '0'; } return addDigits(temp); } };
最后
以上就是开朗鸡最近收集整理的关于LeetCode 258 各位相加[递归] HERODING的LeetCode之路的全部内容,更多相关LeetCode内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复