Roman to Integer Leetcode

Source: Internet
Author: User

Given a Roman numeral, convert it to an integer.

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

Subscribe to see which companies asked this question

Ⅰ (1) Ⅴ (5) Ⅹ (Ten) L (+) C (+) D (+) M (1000)

Rule: At the back of a large number as a addend, in the front of the large number as a meiosis

such as: ⅲ=3,ⅳ=4,ⅵ=6,ⅹⅸ=19,ⅹⅹ=20,ⅹlⅴ=45,mcmⅹⅹc=1980

Starting from the lowest bit, if the previous number is larger than it, let the previous value precede it, and if it is smaller, let the current value minus the previous value

intRomantoint (strings) {if(s.length () = =0)return 0; intLen =s.length (); Unordered_map<Char,int>map; Map.insert (Make_pair ('I',1)); Map.insert (Make_pair ('V',5)); Map.insert (Make_pair ('X',Ten)); Map.insert (Make_pair ('L', -)); Map.insert (Make_pair ('C', -)); Map.insert (Make_pair ('D', -)); Map.insert (Make_pair ('M', +)); intresult = map.at (s.at (Len-1)); intPivot =result;  for(inti = len-2; I >=0; i--) {        intCurr =map.at (s.at (i)); if(Curr >=pivot) {Result+=Curr; }        Else{result-=Curr; } Pivot=Curr; }    returnresult;}

Roman to Integer Leetcode

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.