12. Integer to Roman | LeetCode | Top Interview 150 | Coding Questions

Published: (December 25, 2025 at 04:53 PM EST)
1 min read
Source: Dev.to

Source: Dev.to

https://leetcode.com/problems/integer-to-roman/

leetcode 12

Solution

class Solution {
    public String intToRoman(int num) {

        int[] values = {
            1000, 900, 500, 400,
            100, 90, 50, 40,
            10, 9, 5, 4,
            1
        };

        String[] symbols = {
            "M", "CM", "D", "CD",
            "C", "XC", "L", "XL",
            "X", "IX", "V", "IV",
            "I"
        };

        StringBuilder ans = new StringBuilder();

        for (int i = 0; i = values[i]) {
                num -= values[i];
                ans.append(symbols[i]);
            }
        }

        return ans.toString();
    }
}
Back to Blog

Related posts

Read more »