算法笔记: 力扣#258 各位相加

问题描述


解法


分析

Python 实现

1
2
3
4
5
6
7
8
9
10
11
class :
def addDigits(self, num):
"""
:type num: int
:rtype: int
"""
if num == 0:
return 0
if num % 9 == 0:
return 9
return num % 9

Java 实现

1
2
3
4
5
6
7
class {
public int addDigits(int num) {
if(num == 0){ return 0; }
if(num % 9 == 0){ return 9; }
return num % 9;
}
}

时间复杂度

O(1).

空间复杂度

O(1).

链接


258. Add Digits
258. 各位相加
(English version) Algorithm Notes: Leetcode#258 Add Digits