我是靠谱客的博主 开朗鸡,这篇文章主要介绍LeetCode 258 各位相加[递归] HERODING的LeetCode之路,现在分享给大家,希望可以做个参考。

在这里插入图片描述

解题思路

当num<10直接返回,否则返回addDigits(num各位的和),用递归可以很轻松解决这道题,代码如下:

代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
class 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内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(79)

评论列表共有 0 条评论

立即
投稿
返回
顶部