Advertisement
Guest User

Integer to Roman Leetcode

a guest
Aug 21st, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.47 KB | None | 0 0
  1. // https://leetcode.com/problems/integer-to-roman/description/
  2. public class Solution {
  3.     public String intToRoman(int num) {
  4.         if (num <= 10) return toString(num);
  5.          
  6.         // we first get the num of digits of the integer
  7.         int temp = num;
  8.         int nDigits = -1; // 1 less than actual number
  9.         while (temp > 0) {
  10.             nDigits++;
  11.             temp /= 10;
  12.         }
  13.          
  14.         // create an empty string
  15.         StringBuilder sb = new StringBuilder();
  16.          
  17.         // parse the integer from left to right
  18.         while (num > 0) {
  19.             int place = (int) Math.pow(10, nDigits--);
  20.             int digit = num / place;
  21.             num -= digit * place;
  22.              
  23.             if (digit == 4 || digit == 9) {
  24.                 sb.append(toString(place));
  25.                 sb.append(toString((digit + 1) * place));
  26.             } else if (digit == 5) {
  27.                 sb.append(toString(place * digit));
  28.             } else if (digit < 4){
  29.                 for (int i = 0; i < digit; i++) {
  30.                     sb.append(toString(place));
  31.                 }
  32.             } else if (digit > 5) {
  33.                 sb.append(toString(5 * place));
  34.                 for (int i = 0; i < digit - 5; i++) {
  35.                     sb.append(toString(place));
  36.                 }
  37.             }
  38.         }
  39.          
  40.         return sb.toString();
  41.     }
  42.      
  43.     private String toString(int num) {
  44.         String roman = "";
  45.         switch(num) {
  46.             case 1: roman = "I";
  47.                     break;
  48.             case 2: roman = "II";
  49.                     break;
  50.             case 3: roman = "III";
  51.                     break;
  52.             case 4: roman = "IV";
  53.                     break;
  54.             case 5: roman = "V";
  55.                     break;
  56.             case 6: roman = "VI";
  57.                     break;
  58.             case 7: roman = "VII";
  59.                     break;
  60.             case 8: roman = "VIII";
  61.                     break;
  62.             case 9: roman = "IX";
  63.                     break;
  64.             case 10: roman = "X";
  65.                      break;
  66.             case 50: roman = "L";
  67.                      break;
  68.             case 100: roman = "C";
  69.                       break;
  70.             case 500: roman = "D";
  71.                       break;
  72.             case 1000: roman = "M";
  73.                        break;
  74.             default: roman = "Invalid";
  75.                      break;
  76.         }
  77.         return roman;
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement