Leetcode 13. Roman to Integer string

Source: Internet
Author: User

Roman to Integer


Given a Roman numeral, convert it to an integer.

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


Added: Roman numerals

There are seven Roman numerals, namely I (1), V (5), X (Ten), L (+), C (+), D (+), M (1000). Any positive integer can be represented by the following rules.

Repeat several times: a Roman number repeats several times, indicating several times the number.


Right plus left minus: a smaller Roman number on the right side of a larger Roman numeral, indicating a large number plus a small number. A smaller Roman number on the left side of a larger number indicates a large number to reduce the number. However, the left subtraction cannot span the hierarchy. For example, 99 can not be used to express the IC, with XCIX.


Add line multiply: Add a dash above a Roman numeral or write m at the bottom right, indicating that this number is multiplied by 1000, which is 1000 times times the original number. Similarly, if there are two horizontal lines above, that is 1 million times times the original number.


Unit limit: The same unit can only appear 3 times, such as 40 can not be expressed as XXXX, but to be represented as XL.



Ideas:

    1. Divides a string into 3 parts, left,mid,right.

    2. Gets the maximum unit m for the current string. Record position, character.

    3. Gets the maximum number of consecutive occurrences of the unit N.

    4. By recursion, the result is m*n-left + right.


The code is as follows:

Int romantoint (string s)  {if  (S.size ()  == 0) Return 0;string left,  right;map<char, int> romanmap;romanmap.insert (pair<char, int> (' I ',  1)); Romanmap.insert (pair<char, int> (' V ',  5)); Romanmap.insert (pair<char, int> (' X ',  Romanmap.insert (pair<char, int> (' L ',  50)); Romanmap.insert (pair<char, int> (' C '),  100)); Romanmap.insert (pair<char, int> (' D ',  500)); Romanmap.insert (Pair<char, int > (' M ',  1000) int maxgrade = 0; //char maxchar =  ';int  maxgrades = 0;//int maxgradepos = 0; //superlative position for  (int i =  0; i < s.size ();  i++)//Gets the highest-level character, the highest-level position {if  (romanmap[s[i]] >  Maxgrade) {maxgrade = romanmap[s[i]];maxchar = s[i];maxgradepos = i;}} for  (int i = maxgrAdepos; i < s.size ();  i++)//Get the highest number of consecutive {if  (S[i] == maxchar) maxGrades++; Elsebreak;} Left = s.substr (0, maxgradepos); Right = s.substr (maxgradepos + maxgrades); Return maxgrades * maxgrade - romantoint (left)  + romantoint (right);}


Refer to other people's practices:

Reference URL: http://blog.csdn.net/feliciafay/article/details/17259547

The small rules that observe the conversion of Roman numerals and integers are:

IV = 5-1 = (-1) + 5 = 4

VI = 5 + 1 = 5 + 1 = 6

I is in front of V, because I is smaller than V, so I is interpreted as a negative number.

V is behind I, because V is larger than I, so v gives an explanation for an integer.

Keep looking at a few examples.

VII = 5 + 1 + 1 = 7

IX = (-1) + 10 = 9

Therefore, you can scan the input string one at a time, starting with the first character, up to the last character, and comparing the current character A and the next character B of the current character at a time. If a< B, interpreted as b-a, otherwise if a >= B, is interpreted as a + B. In fact, because each time the 2 adjacent characters are always compared, the subscript is starting from 0 and ending with inputstring.length ()-2.

The code is as follows:

Class solution {public:    int romantoint (string s)  {     int sum = 0;          int  Start = 0;            char char_arr [] = {' I ',  ' V ',  ' X ',  ' L ',  ' C ',  ' D ',  ' M '};     &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;INT&NBSP;INT_ARR[]&NBSP;=&NBSP;{1,&NBSP;5,&NBSP;10,&NBSP;50,&NBSP;100,  500, 1000};          std::map<char, int > roman_map;          int map_len =  sizeof (Char_arr)/sizeof (char);           for  (int  i = 0; i< map_len; ++i)                 roman_map.insert (std::p air<char, int>  (char_arr[i], int_arr[i));           for  (int i = 0; i <  S.length ()  - 1; ++i)  {               if  (roman_map[s[i]]>=roman_map[s[i + 1])                    sum += roman_map[s[i]] ;              else                   sum -=  roman_map[s[i]];          }           sum += roman_map[s[s.length () -1]];           return sum;      }}; 


2016-08-10 15:44:23

This article is from the "Do Your best" blog, so be sure to keep this source http://qiaopeng688.blog.51cto.com/3572484/1836563

Leetcode 13. Roman to Integer string

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.