Integer to Roman

LeetCode Q 12 - Integer to Roman

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.

Solution

Code:

public String intToRoman(int num) {
  StringBuilder sb = new StringBuilder();
  while (num != 0) {
    if (num >= 1000) {
      sb.append('M'); num -= 1000;
    } else if (num >= 900) {
      sb.append("CM"); num -= 900;
    } else if (num >= 500) {
      sb.append("D"); num -= 500;
    }  else if (num >= 400) {
      sb.append("CD"); num -= 400;
    } else if (num >= 100) {
      sb.append("C"); num -= 100;
    } else if (num >= 90) {
      sb.append("XC"); num -= 90;
    } else if (num >= 50) {
      sb.append("L"); num -= 50;
    }  else if (num >= 40) {
      sb.append("XL"); num -= 40;
    } else if (num >= 10) {
      sb.append("X"); num -= 10;
    } else if (num >= 9) {
      sb.append("IX"); num -= 9;
    } else if (num >= 5) {
      sb.append("V"); num -= 5;
    } else if (num >= 4) {
      sb.append("IV"); num -= 4; 
    } else if (num >= 1) {
      sb.append("I"); num -= 1;
    } 
  }
  return sb.toString();
}

Code: A concise version

public int reverse(int x) {	
  String[] thous = {"", "M", "MM", "MMM"};
  String[] huns = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
  String[] tens = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
  String[] ones = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
  
  return thous[num / 1000] + huns[num / 100 % 10] + tens[num / 10 % 10] + ones[num % 10];
}

   Reprint policy


《Integer to Roman》 by Tong Shi is licensed under a Creative Commons Attribution 4.0 International License
  TOC