algorithm notes: leetcode#258 add digits

Problem


Solution


Analysis

Python implementation

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 implementation

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;
}
}

Time complexity

O(1).

Space complexity

O(1).


258. Add Digits
(中文版) 算法笔记: 力扣#258 各位相加