Leetcode | | Roman to Integer problem

Source: Internet
Author: User

Problem:

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

Convert Roman numerals to integers


Thinking:

(1) Roman numeral rules:

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. It is generally believed that Roman numerals are used only for counting, not for calculation. The
repeats several times: a Roman number repeats several times, representing several times the number.
Right plus left:
The smaller Roman numerals on the right side of the larger Roman numerals, indicating large numbers plus small numbers.
the smaller Roman numerals on the left side of the larger Roman numerals indicate that large numbers decrease the numbers. The number of
minus left is limited to I, X, C. For example, 45 can not be written as VL, only XLV
However, the left minus time cannot span a single digit. For example, 99 may not be represented by an IC (100-1), but rather by XCIX ([100-10] + [10-1]). (equivalent to each digit of the Arabic numerals respectively.) The
left minus number must be a bit, such as 8 written in VIII, not IIX. The
Right plus number cannot be more than three consecutive digits, for example, 14 is written as XIV, not XIIII. (See "Digital Restrictions" below.)
Add line multiply:
Add a line or subscript to the top of the Roman numeral, which means multiply this number by 1000, which is 1000 times times the original number.
Similarly, if there are two horizontal lines above, it is 1000000 (1000^{2}) 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 (i.e. Ivpiter, the Roman alphabet without J and u), it is sometimes substituted with iiii instead of Ⅳ.

Code

Class Solution {public:    int Romantoint (string s) {        //Note:the Solution Object was instantiated only once and is R eused by each test case.        int result=0;                Map<char,int> Roman;        roman[' I ']=1;        roman[' V ']=5;        roman[' X ']=10;        roman[' L ']=50;        roman[' C ']=100;        roman[' D ']=500;        roman[' M ']=1000;                for (int i=s.length () -1;i>=0;i--)        {            if (i==s.length ()-1)            {                result=roman[s[i]];                Continue;            }            if (Roman[s[i]] >= roman[s[i+1]])                result+=roman[s[i];            else                Result-=roman[s[i]];        }        return result;}    ;


Leetcode | | Roman to Integer problem

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