Concise 7m Java solution


  • 1
    S
    public class Solution {
      private static final int[] vals =
        new int[] { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
    
      private static final String[] strs =
        new String[] { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
        
      public String intToRoman(int num) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; num > 0; i++) {
          for ( ; num >= vals[i]; sb.append(strs[i]), num -= vals[i]);
        }
        return sb.toString();
      }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.