Leetcode -- Roman to integer

Source: Internet
Author: User

Problem description:

Given a roman numeral, convert it to an integer.

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

Analysis: the question refers to converting a number in Rome into an integer. First, you can find the following rule on the Internet:

1. Counting Method : ① the roman numerals have the following seven basic symbols: I (1), V (5), X (10), L (50), C (100), D (500), M (1000 ).

② if the numbers are the same, the numbers are equal to the total number of the numbers, for example, ⅲ = 3; xx = 20; CC = 200; Mmm = 3000;

③ a small number is on the right of a large number, and the number expressed is equal to the sum of these numbers, for example, item 8 = 8; period = 12;

④ a small number (limited to I, X, and c) on the left of a large number. The number is equal to the number of A large number to be reduced, for example: IV = 4; ix = 9;

⑤ The number repeatedly written during normal use cannot exceed three times;


2. Group number rules: ① No more than three of the basic numbers I, X, and C can be used to form a number or placed on the right of a large number; you can only use one value to the left of a large number.

② Do not place any of the basic numbers V, L, and D as decimal places on the left of the large number and subtract them to form the number. add them to the right of the large number to form the number;

③ The small numbers on the left of V and X can only be I.

④ The small numbers on the left of L and C can only use X.

⑤ The small numbers on the left of D and M can only use C.


There are also rules that need to be considered when the integer is greater than 3999. According to the above rules, it can be seen that when the number of the Roman numerals into an integer, if the current number temp is less than or equal to the left of the number last, then directly add; otherwise it should be added temp-2 * last; according to this rule, write the conversion Code as follows:

Class solution {public: int rtoi (char ch) {Switch (CH) {Case 'I': return 1; Case 'V': return 5; Case 'X ': return 10; Case 'l': return 50; Case 'C': return 100; Case 'D': return 500; Case 'M': Return 1000; default: return 0 ;}} int romantoint (string s) {int res = 0; If (S. empty () return 0; int temp, last; temp = rtoi (s [0]); last = temp; Res + = last; For (INT I = 1; I <S. size (); ++ I) {temp = rtoi (s [I]); If (temp <= last) RES + = temp; else res + = temp-2 * last; last = temp;} return res ;}};


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.