"Leetcode" Roman to Integer

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.

First, learn 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 (+). Any positive integer can be expressed according to the following rules. It is important to note that there is no "0" in Roman numerals, and it is not related to an advanced system. It is generally believed that the Roman numerals are used only in memory, not in calculus.

    • Repeat several times: a Roman number repeats several times, representing several times this number.
    • Right plus left minus:
      • On the right side of the larger Roman numerals, the smaller Roman numerals indicate large numbers plus small numbers.
      • The smaller Roman numerals on the left side of the larger Roman numerals indicate that the large numbers decrease the number of words.
      • 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 minus time cannot be crossed by one digit. For example, 99 can not be represented by IC (100-1), but by Xcix ((100-10) + (10-1)). (equivalent to the Arabic numerals, each digit of the word.) )
      • The left subtraction must be one , for example, 8 written into VIII, not IIX.
      • The right plus number is not allowed to continue over three bits, such as 14 as XIV rather than XIIII. (see "Number Limits" below.) )
    • Add line by thousand:
      • Add a horizontal line or a marker to the top of the Roman numerals, which 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 (1000^2) times the original number.
    • Number limit:
      • The same number can only appear at most three times, such as 40 can not be expressed as XXXX, but to be represented as XL.
      • Exception: Since IV is the first word of the ancient Roman god, the Lord God of Jupiter (i.e., the ivpiter, there is no J and U in the Ancient Roman alphabet), it is sometimes used IIII instead of Ⅳ.

Here's a very interesting question:

For this problem, the really useful rules are only the black part of the above, then we are "from the past" scan calculation or "forward" scan it?

* If it is from the go, then assume that the scanned location has been processed (that is, the value of this bit has been calculated):

1. When s[i] > s[i-1], it should be s[i]-s[i-1], but at this time s[i-1] has been processed and must have been added to the ANS (s[i-2] Sub-case discussion will know), so at this time need ans + = s[i]-2*s[i-1];

2. When S[i] <= s[i-1], it should be added s[i-1], this is no problem.

* If it is from the back, then assume that the scanned position has been processed (that is, the value of this bit has been calculated):

1. When s[i] >= s[i+1], the s[i] added (s[i+1] is added to ans or reduced to ans is not affected), the sample has XCIX (99);

2. When s[i] < s[i+1], reduce the s[i] into ans.

So, in contrast, the "from behind" strategy is more normative and unified, logically clearer, ans in the process of the fluctuations in theory is also smaller, and it is consistent with our assumption that only the bits that have not been scanned, have been scanned no longer do any "remedial" form of processing.

Below, the code for both strategies is posted:

"Back to Back" strategy:

1 classSolution:2     #@param {string} s3     #@return {integer}4     defRomantoint (self, s):5Roman = {6             "I": 1,7             "V": 5,8             "X": 10,9             "L": 50,Ten             "C": 100, One             "D": 500, A             "M": 1000 -             } -Ans =Roman[s[0]] the          forIinchRange (1, Len (s)): -             ifRoman[s[i]] > Roman[s[i-1]]: -Ans + = roman[s[i]]-2*roman[s[i-1]] -             Else: +Ans + =Roman[s[i]] -         returnAns

"Forward from the back" strategy:

1 classSolution:2     #@param {string} s3     #@return {integer}4     defRomantoint (self, s):5Roman = {6             "I": 1,7             "V": 5,8             "X": 10,9             "L": 50,Ten             "C": 100, One             "D": 500, A             "M": 1000 -             } -n =Len (s) theAns = roman[s[n-1]] -i = N-2 -          whileI >=0: -             ifRoman[s[i]] >= roman[s[i+1]]: +Ans + =Roman[s[i]] -             Else: +Ans-=Roman[s[i]] AI-= 1 at         returnAns

Reference:

http://blog.csdn.net/jellyyin/article/details/13165731

"Leetcode" Roman 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.