class Solution:
    def intToRoman(self, num: int) -> str:
        answer = ""
        ones = ["M", "C", "X", "I"]
        fives = ["D", "L", "V"]

        # Handle thousands
        thou = num // 1000
        i = 0
        while i < thou:
            answer += "M"
            i = i + 1

        # Remove thousands
        num %= 1000
        num = str(num)
        
        # Padding to fill 3 digits
        while len(num) != 3:
            num = "0" + num

        # Iterate num from left to right
        for i, x in enumerate(num):
            x = int(x)
            if x == 5:
                answer += fives[i]
            elif x == 9:
                answer += ones[i + 1]
                answer += ones[i]
            elif x == 4 :
                answer += ones[i + 1]
                answer += fives[i]
            elif x < 5:
                answer += (x * ones[i+1])
            elif x > 5:
                answer += fives[i]
                answer += ((x - 5) * ones[i+1])
                
        return answer

'CS > 알고리즘' 카테고리의 다른 글

[Leetcode] 15. 3Sum  (0) 2023.05.01
[Leetcode] 1. Two Sum  (0) 2023.05.01
[Leetcode] 202. Happy Number  (0) 2023.04.27
[Leetcode] 55. Jump Game  (0) 2023.04.27
[Leetcode] 11. Container With Most Water  (0) 2023.04.27