LeetCode Roman to Integer, leetcoderoman

Source: Internet
Author: User

LeetCode Roman to Integer, leetcoderoman

Given a roman numeral, convert it to an integer.

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

The meaning of the question is to convert the roman numerals into Arabic numerals.

Idea: After understanding the structure of the Roman numerals, you can proceed from the back to the front.

Class Solution {public: int romanToInt (string s) {if (s. size () = 0) return 0; map <char, int> mp; mp ['I'] = 1; mp ['V'] = 5; mp ['X'] = 10; mp ['l'] = 50; mp ['C'] = 100; mp ['D'] = 500; mp ['M'] = 1000; int len = s. size (); int sum = mp [s [len-1]; for (int I = len-2; I> = 0; I --) {if (mp [s [I] <mp [s [I + 1]) sum-= mp [s [I]; else sum + = mp [s [I];} return sum ;}};
















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.