Roman to Integer (easy)

Source: Internet
Author: User


Given a Roman numeral, convert it to an integer.

Input is guaranteed to being within the range from 1 to 3999.

A brief introduction to Roman numerals, excerpted from Wikipedia

There are 7 Roman numerals, namely I (1), V (5), X (10), L (50), C (100), D (500) and M (1000). Any positive integer can be represented by the following rules. It is important to note that there is no "0" in the Roman numerals, regardless of the rounding system. Roman numerals are generally considered to be used only for counting, not for calculation.

    • Repeat several times: a Roman number repeats several times, indicating several times the number.
    • Right plus left minus:
      • The smaller Roman numerals on the right side of the larger Roman numerals indicate large numbers plus small numbers.
      • The smaller Roman numerals on the left of the larger Roman numerals indicate that large numbers decrease the numbers.
      • The left-minus number is limited to I, X, and C. For example, 45 can not be written as VL, only XLV
      • However, the left subtraction cannot span a single digit. For example, 99 cannot be represented by an IC (), but by XCIX (). (equivalent to each digit of the Arabic numerals.) )
      • The left minus number must be one, such as 8 written in VIII, not IIX.
      • The right plus number cannot be more than three consecutive digits, such as 14 for XIV rather than XIIII. (See "Digital Restrictions" below.) )
    • Add line by thousand:
      • By adding a horizontal line or a subscript to the Roman numerals, it means multiplying this number by 1000, which is 1000 times times the original number.
      • Similarly, if there are two horizontal lines above, it is 1000000 () times the original number.
    • Digital Restrictions:
      • The same digital can only appear at most three times, such as 40 can not be represented as XXXX, but to be represented as XL.
      • Exception: Since IV is the first word of the Roman mythology Lord Jupiter (ie, ivpiter, the Roman alphabet does not have J and u), it is sometimes substituted with IIII for IV.


1 /* 2 Use HashMap
3 */
Import Java.util.HashMap;
Import Java.util.Map;
4  Public intRomantoint (String s) {5         if(s = =NULL|| S.length () ==0)return0;6Map<character, integer> m =NewHashmap<character, integer>();7M.put (' I ', 1);8M.put (' V ', 5);9M.put (' X ', 10);TenM.put (' L ', 50); OneM.put (' C ', 100); AM.put (' D ', 500); -M.put (' M ', 1000); -  the         intLength =s.length (); -         intresult = M.get (S.charat (length-1));// from right to left -          for(inti = length-2; I >= 0; i--) {//Backwards -             if(M.get (S.charat (i + 1)) <=M.get (S.charat (i))) +Result + =M.get (S.charat (i)); -             Else  +Result-=M.get (S.charat (i)); A         } at         returnresult; -}

Run time:460ms

Roman to Integer (easy)

Contact Us

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: 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.