forked from Srinivas11789/AlgorithmNuggets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd.py
More file actions
22 lines (20 loc) · 686 Bytes
/
add.py
File metadata and controls
22 lines (20 loc) · 686 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution(object):
def addDigits(self, num):
"""
:type num: int
:rtype: int
"""
# O(1) Logic - Solution: Digital Root
# https://en.wikipedia.org/wiki/Digital_root
if num == 0:
return 0
else:
# Thought of a logic close to --> single digit == 0-9, hence if sum is greater than 9, then use the difference to calculate something -- Nice thought!! ==> 38 = 3+8 = 11 = 11-9 = 2
return 1+(num-1)%9
"""
# Loop logic
num = map(int,str(num))
while len(num) > 1:
num = map(int,str(sum(num)))
return num[0]
"""