[Leetcode] [JavaScript] Roman to Integer

Source: Internet
Author: User

Roman to Integer

Given a Roman numeral, convert it to an integer.

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

https://leetcode.com/problems/roman-to-integer/

Roman numerals turn to Arabic numerals.

Sweep forward, if the current number is greater than the previous number, plus this number, and vice versa minus the current number.

1 /**2 * @param {string} s3 * @return {number}4  */5 varRomantoint =function(s) {6     varMap = {};7map["I"] = 1; map["V"] = 5; map["X"] = 10; map["L"] = 50;8map["C"] = 100; map["D"] = 500; map["M"] = 1000;9     varRES, TMP = 0;Ten      for(vari = s.length-1; I >= 0; i--){ One         if(!Res) { Ares =Map[s[i]]; -             Continue; -         } the         if(Map[s[i]] >= map[s[i + 1]]){ -Res + =Map[s[i]]; -}Else{ -Res-=Map[s[i]]; +         } -     } +     returnRes; A};

[Leetcode] [JavaScript] Roman to Integer

Related Article

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.