LeetCode 258. 各位相加(Add Digits)

2023-04-28,,

258. 各位相加

258. Add Digits

题目描述

给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。

LeetCode258. Add Digits

示例:

输入: 38
输出: 2
解释: 各位相加的过程为: 3 + 8 = 11, 1 + 1 = 2。 由于 2 是一位数,所以返回 2。

进阶:

你可以不使用循环或者递归,且在 O(1) 时间复杂度内解决这个问题吗?

Java 实现

class Solution {
public int addDigits(int num) {
return 1 + (num - 1) % 9;
}
}

相似题目

202. 快乐数

参考资料

https://leetcode-cn.com/problems/add-digits/
https://leetcode.com/problems/add-digits/

LeetCode 258. 各位相加(Add Digits)的相关教程结束。

《LeetCode 258. 各位相加(Add Digits).doc》

下载本文的Word格式文档,以方便收藏与打印。