Leetcode | Roman to Integer

Source: Internet
Author: User

Roman to integer:https://leetcode.com/problems/roman-to-integer/
Degree:easy

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

Roman numerals: http://zh.wikipedia.org/wiki/%E7%BD%97%E9%A9%AC%E6%95%B0%E5%AD%97

The key is to understand the composition of Roman numerals: Appendix Below

//algorithm time complexity O (n), Space complexity O (1)a total of 7 Roman numerals, i.e. I (1), V (5), X (10), L (50), C (100), D (500) and M (+)//On the right side of the larger Roman numerals, the smaller Roman numerals indicate large numbers plus small numbers. //On the left of the larger Roman numerals, the smaller Roman numerals indicate that large numbers decrease the numberClass Solution { Public:int Romantoint(strings) {if(s.size () = =0)return 0;intK = S.size ()-1;intPrev = Chartoint (S[k]);//Last digit        intresult = prev; while(--k >=0) {intcur = chartoint (s[k]); Result + = (cur >= prev)?            Cur:-cur;        prev = cur; }returnResult }Private:int Chartoint(Charc) {Switch(c) { Case ' I ':return 1; Case ' V ':return 5; Case ' X ':return Ten; Case ' L ':return  -; Case ' C ':return  -; Case ' D ':return  -; Case ' M ':return  +;default:return 0; }    }};

Appendix: Roman Numeral Spelling Rules:
There are 7 Roman numerals, namely I (1), V (5), X (10), L (50), C (100), D (500) and M (1000). Any positive integer can be represented by the following rules. It is important to note that there is no "0" in the Roman numerals, regardless of the rounding system. Roman numerals are generally considered to be used only for counting, not for calculation.

 - 重复数次:一个罗马数字重复几次,就表示这个数的几倍。 - 右加左减:    在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字。    在较大的罗马数字的左边记上较小的罗马数字,表示大数字减小数字。    左减的数字有限制,仅限于I、X、C。比如45不可以写成VL,只能是XLV    但是,左减时不可跨越一个位数。比如,99不可以用IC(100 - 1)表示,而是用XCIX([100 - 10] + [10 - 1])表示。(等同于阿拉伯数字每位数字分别表示。)    左减数字必须为一位,比如8写成VIII,而非IIX。    右加数字不可连续超过三位,比如14写成XIV,而非XIIII。(见下方“数码限制”一项。) - 加线乘千:    在罗马数字的上方加上一条横线或者加上下标的?,表示将这个数乘以1000,即是原数的1000倍。    同理,如果上方有两条横线,即是原数的1000000(1000^{2})倍。 - 数码限制:    同一数码最多只能出现三次,如40不可表示为XXXX,而要表示为XL。    例外:由于IV是古罗马神话主神朱庇特(即IVPITER,古罗马字母里没有J和U)的首字,因此有时用IIII代替IV。

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.