13Roman to Integer

Source: Internet
Author: User

Roman to Integer

Links: https://leetcode.com/problems/roman-to-integer/
Problem Description:
Given a Roman numeral, convert it to an integer.

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

Hide Tags Math String

The problem is to convert Roman numerals to Arabic numerals. First look at the rules of Roman numerals.
There are 7 Roman numerals, namely I (1), V (5), X (10), L (50), C (100), D (500) and M (1000).
1, repeated several times: a Roman numeral repeated several times, it means that the number of several times.

2. Right plus left minus:
2.1 The smaller Roman numerals on the right side of the larger Roman numerals, indicating large numbers plus small numbers.
2.2 The smaller Roman numerals on the left of the larger Roman numerals, indicating that large numbers decrease the numbers.
2.3 The left minus number is limited to I, X, C. For example, 45 can not be written as VL, only XLV
2.4 However, the left minus time cannot span one 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.) )
2.5 The left minus number must be one, such as 8 written in VIII, not IIX.
2.6 The right plus number cannot be more than three consecutive digits, such as 14 written in XIV rather than XIIII. (See "Digital Restrictions" below.) )

3, add line by thousand:
3.1 By adding a horizontal line or subscript on top of the Roman numerals, it means multiplying this number by 1000, which is 1000 times times the original number.
3.2 Similarly, if there are two horizontal lines above, it is 1000000 (1000^{2}) times the original number.

4, Digital restrictions:
4.1 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.
4.2 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 instead of Ⅳ.

With the rules then the program becomes very simple, just find the number that needs to be subtracted, the other numbers are added.

class Solution {public:    int romanToInt(string s)     {        if(s=="") return 0;        int *n=new int[s.length()];        int num=0;        for(int i=0;i<s.length();i++)        {            if(s[i]==‘I‘)            n[i]=1;else if (s[i]==‘V‘)            n[i]=5;else if (s[i]==‘X‘)            n[i]=10;else if (s[i]==‘L‘)            n[i]=50;else if (s[i]==‘C‘)            n[i]=100;else if (s[i]==‘D‘)            n[i]=500;else if (s[i]==‘M‘)            n[i]=1000;        }        for(int i=0;i<s.length()-1;i++)        {            if(n[i]<n[i+1]&&(n[i]==1||n[i]==10||n[i]==100))            num=num-n[i];               else            num+=n[i];        }        num+=n[s.length()-1];        delete n;        return num;    }};

13Roman to Integer

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.