LeetCode 1556. Thousand Separator Solution in Java, C++, Python & Go | Explanation + Code

CoderIndeed
0
1556. Thousand Separator

Description

Given an integer n, add a dot (".") as the thousands separator and return it in string format.

 

Example 1:

Input: n = 987
Output: "987"

Example 2:

Input: n = 1234
Output: "1.234"

 

Constraints:

  • 0 <= n <= 231 - 1

Solutions

Solution 1

PythonJavaC++Go
class Solution: def thousandSeparator(self, n: int) -> str: cnt = 0 ans = [] while 1: n, v = divmod(n, 10) ans.append(str(v)) cnt += 1 if n == 0: break if cnt == 3: ans.append('.') cnt = 0 return ''.join(ans[::-1])(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !