describe
Given an integer n, add a dot (“.”) as the thousands separator and return it in string format.
Example 1:
Input: n = 987
Output: "987"
Copy the code
Example 2:
Input: n = 1234
Output: "1.234"
Copy the code
Example 3:
Input: n = 123456789
Output: "123.456.789"
Copy the code
Example 4:
Input: n = 0
Output: "0"
Copy the code
Example 5:
Note:
0 <= n < 2^31
Copy the code
parsing
The number n is converted to a string, separated by dots every three places, and then returned with a new string.
answer
class Solution(object):
def thousandSeparator(self, n):
"""
:type n: int
:rtype: str
"""
s = str(n)
res = []
for i in range(len(s), -1, -3):
tmp = s[max(0, i - 3):i]
if tmp:
res.insert(0, tmp)
return ".".join(res)
Copy the code
The results
In the given Python online submission for Thousand Separator. Memory Usage: Submissions in Python online submissions for Thousand Separator.Copy the code
Original link: leetcode.com/problems/th…
Your support is my biggest motivation