LeetCode

Integer to Roman

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.

Java

public class Solution {
    enum Numerial {
        M(1000), CM(900), D(500), CD(400), C(100), XC(90), L(50), XL(40), X(10), IX(9), V(5), IV(4), I(1);
        private final int weight;
        private Numerial(int weight) {
            this.weight = weight;
        }
        public int getWeight() {
            return this.weight;
        }
    }
    public String intToRoman(int num) {
        StringBuilder result = new StringBuilder();
        final Numerial[] values = Numerial.values();
        for(int i = 0; i <  values.length; i++) {
            while(num >= values[i].getWeight()) {
                result.append(values[i]);
                num -= values[i].getWeight();
            }
        } 
        return result.toString();
    }
}