# Leetcode:integer to Roman

Source: Internet
Author: User

1. Title

Integer to Roman (conversion of Arabic numerals to Roman numerals)

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

3. Topic content

English: Given An integer, convert it to a Roman numeral. Input is guaranteed to being within the range from 1 to 3999.

English: Give an integer and convert it to Roman numerals. The input is between 1-3999.

4, the problem analysis

To convert Arabic numerals to Roman numerals, you first need to understand the rules for generating Roman numerals. There are 7 Roman numerals, namely I (1), V (5), X (10), L (50), C (100), D (500) and M (1000), and its generation rules are more complex and can be referenced in Wikipedia articles: Roman Numerals (Chinese | english). Although writing Roman numerals is a trivial matter, converting decimal numbers to Roman numerals has a simple rule that can be seen in the following table:

As can be seen, compared with the decimal number, although the number of bits, 10, hundred, thousands of these digits on the Roman numerals are different, but all have a common law. From a longitudinal comparison, the notation of each digit is related only to the number of times, five times times, and 10 times times the corresponding letters of the digits. The Roman numerals such as 8 are VIII (5+1+1+1), 80 are lxxx (50+10+10+10), and 800 are DCCC (500+100+100+100). So we can take out the decimal numbers and generate the corresponding Roman numerals according to the character generation rules of the Roman numerals.

5. Method of solving Problems 1

Following the analysis in section fourth, the implementation code is as follows:

`/** *  function Description:leetcode 12 - integer to roman *  Developer: Tsybius *   Development Time: August 2, 2015  */public class Solution {         /**     *  Arabic numerals to Roman numerals (3999 and below)      *  @param  num  converted Arabic numerals      *  @return   converted Roman numerals       */    public string inttoroman (Int num)  {         String result =  "";         if  (num >= 1000)  {             result += getromanmultipleof1000 (num - num % 1000);             num %= 1000;         }        if  (num >= 100)  {       &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RESULT&NBSP;+=&NBSP;GETROMANMULTIPLEOF100 (num - num %  (+);            num %= 100;         }        if  (num >= &NBSP;10)  {            result +=  GETROMANMULTIPLEOF10 (num - num % 10);             num %= 10;        }                 result +=  GETROMANLOWEREQUALTHAN10 (num);                 return result;    }        /**     *   Roman numerals less than 10      *  @param  num     * @ Return     */    public string getromanlowerequalthan10 (int num)  {        switch (num)  {             case 1: return  "I";             case 2: return  "II";             case 3: return  "III";             case 4: return  "IV";             case 5: return  "V";             case 6: return  "VI";             case 7: return  "VII";             case 8: return  "VIII";             case 9: return  "IX";             default: return  "";        }   Multiples of   }        /**     * 10      *  @param  num     *  @return    &NBSP;&NBSP;&NBSP;*/&NBSP;&NBSP;&NBSP;&NBSP;PUBLIC&NBSP;STRING&NBSP;GETROMANMULTIPLEOF10 (Int num)  {         switch (num)  {             case 10: return  "X";             case 20: return  "XX";             case 30: return  "XXX";             case 40: return  "XL";             case 50: return  "L";             case 60: return  "LX";             case 70: return  "LXX";             case 80: return  "LXXX";             case 90: return  "XC";             default: return "";        }             }        /**     *  Multiples of 100      *  @param  num     *  @return   &NBSP;&NBSP;&NBSP;&NBSP;*/&NBSP;&NBSP;&NBSP;&NBSP;PUBLIC&NBSP;STRING&NBSP;GETROMANMULTIPLEOF100 (Int num)  {        switch (num)  {             case 100: return  "C";             case 200: return  "CC";             case 300: return  "CCC";             case 400: return  "CD";             case 500: return  "D";             case 600: return  "DC";             case 700: return  "DCC";             case 800: return  "DCCC";             case 900: return  "CM";             default: return  "";        }   Multiples of   }    /**     * 1000       *  @param  num     *  @return      */     public string getromanmultipleof1000 (Int num)  {         sWitch (num)  {            case 1000:  return  "M";            case 2000:  return  "MM";            case 3000:  return  "MMM";             default: return   "";         }    }}`

6. Method of solving Problems 2

The code in Method 1 is too verbose, and similar code is rewritten several times, and we can use arrays to shorten the number of lines of code:

`/** *  function Description:leetcode 12 - integer to roman *  Developer: Tsybius *   Development Time: August 2, 2015  */public class Solution {         /**     *  Arabic numerals to Roman numerals (3999 and below)      *  @param  num  converted Arabic numerals      *  @return   converted Roman numerals       */    public string inttoroman (Int num)  {         String[][] RomanDict = new String[][] {             {  ", " I ", " II ", " III ", " IV ", " V ", " VI ", " VII ", " VIII ", " IX " },             {  ", " X ", " XX ", " XXX ", " XL ", " L ", " LX ",   "LXX",  "LXXX",  "XC" },            { " ", " C ", " CC ", " CCC ", " CD ", " D ", " DC ", " DCC ", " DCCC ", " CM " },             {  "",  "M",  "MM",  "MMM",  "",   ", " ", " ", " ", " ", " "&NBSP;},&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;} ;        return romandict[3][num / 1000] +              romandict[2][num % 1000  / 100] +            romandict[1][ num % 100 / 10] +             romandict[0][num % 10];    }}`

END

Leetcode:integer to Roman

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.