LeetCode solutions

12. Integer to Roman

My accepted Python solution to LeetCode problem 12, Integer to Roman, running in 3ms.

  • Difficulty: Medium
  • Python
  • Runtime 3ms
  • Memory 12.6MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 3ms, memory 12.6MB, accepted 2025-12-23.

python
class Solution(object):
    def intToRoman(self, num):
        val = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
        sym = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
        
        result = ""
        for i in range(len(val)):
            while num >= val[i]:
                result += sym[i]
                num -= val[i]
        
        return result

Source