Leetcode (Roman) to Integer

Source: Internet
Author: User

Topic

Given a Roman numeral, convert it to an integer.
Input is guaranteed to being within the range from 1 to 3999.

Analysis

This topic is a variant of the previous question, which requires the conversion of a given Roman sequence number to the corresponding integer.
Learn about the correspondence between Roman numerals and integers:

The corresponding examples are as follows:

AC Code
Class solution{ Public:Solution() {} ~solution () {}intValue (Charc) {Switch(c) { Case ' I ':return 1; Break; Case ' V ':return 5; Break; Case ' X ':return Ten; Break; Case ' L ':return  -; Break; Case ' C ':return  -; Break; Case ' D ':return  -; Break; Case ' M ':return  +; Break;default:return 0; Break; }    }intRomantoint (strings) {intRET =0;if(s = ="")returnRet RET + = Value (s[0]); for(inti =0; s[i+1] !=' + '; i++) {if(Value (S[i +1]) <= value (S[i]) ret + = Value (S[i +1]);ElseRET = ret + Value (s[i +1]) -2* Value (s[i]); }returnRet }};

Git test code

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.