[Leetcode] Roman to Integer @ Python

Source: Internet
Author: User

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

Given a Roman numeral, convert it to an integer.

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

Ideas: See Code annotations

Code:

classSolution:#@return An integer    defRomantoint (self, s):#from the post-trip scan, record the segmented number with a temporary variable.         #if the current is larger than the previous one, the value of this paragraph should be the current value minus the previous value. such as IV = 5–1;        #Otherwise, the current value is added to the result and the next record is started. such as VI = 5 + 1, ii=1+1        #time complexity O (n), Spatial complexity O (1)Dict = {'M': 1000,'D': 500,'C': 100,'L': 50,'X': 10,'V'75A'I': 1} res=0 forIinchRange (len (s)):ifI > 0 andDict[s[i]] > Dict[s[i-1]]: res+ = Dict[s[i]]-dict[s[i-1]]            Else: Res+=Dict[s[i]]returnRes

[Leetcode] Roman to Integer @ Python

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.