[LeetCode] 1556. Thousand Separator

김민우·2022년 9월 30일
0

알고리즘

목록 보기
26/189

- Problem

1556. Thousand Separator
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 <= 2^31 - 1

- 내 풀이

class Solution:
    def thousandSeparator(self, n: int) -> str:
        s = str(n)[::-1]
        answer = ""
        
        for i, v in enumerate(s):
            if i % 3 == 0 and i != 0:
                answer += "."
            answer += v
        
        return answer[::-1]

- 결과

profile
Pay it forward.

0개의 댓글