forked from YuriSpiridonov/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1556.ThousandSeparator.py
45 lines (38 loc) · 993 Bytes
/
1556.ThousandSeparator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Given an integer n, add a dot (".") as the thousands separator and return
it in string format.
Example:
Input: n = 987
Output: "987"
Example:
Input: n = 1234
Output: "1.234"
Example:
Input: n = 123456789
Output: "123.456.789"
Example:
Input: n = 0
Output: "0"
Constraints:
- 0 <= n < 2^31
"""
#Difficulty: Easy
#69 / 69 test cases passed.
#Runtime: 52 ms
#Memory Usage: 13.9 MB
#Runtime: 52 ms, faster than 25.00% of Python3 online submissions for Thousand Separator.
#Memory Usage: 13.9 MB, less than 25.00% of Python3 online submissions for Thousand Separator.
class Solution:
def thousandSeparator(self, n: int) -> str:
s = str(n)
l = len(s)
nums = []
while s:
if l > 2:
l -= 3
else:
nums.insert(0, s[:l])
break
nums.insert(0, s[l:])
s = s[:l]
return '.'.join(nums)