zoukankan      html  css  js  c++  java
  • [LeetCode][Python]Roman to Integer

    # -*- coding: utf8 -*-
    '''
    __author__ = 'dabay.wang@gmail.com'
    https://oj.leetcode.com/problems/roman-to-integer/
    Roman to Integer

    Given a roman numeral, convert it to an integer.

    Input is guaranteed to be within the range from 1 to 3999.
    ===Comments by Dabay===
    先google一下罗马数字的表示:
    I - 1
    V - 5
    X - 10
    L - 50
    C - 100
    D - 500
    M - 1000
    主要问题是考虑一些4,40之类的表示。

    可以从右往左,依次处理:
    当遇到这个字母表示的数字比后一个小的时候,减去这个数;否则,累加。
    '''

    class Solution:
    # @return an integer
    def romanToInt(self, s):
    roman_dict = {"I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000}
    if len(s) == 0:
    return 0
    value = 0
    for i in xrange(len(s)-1, -1, -1):
    if i < len(s)-1 and roman_dict[s[i]] < roman_dict[s[i+1]]:
    value = value - roman_dict[s[i]]
    else:
    value = value + roman_dict[s[i]]
    return value


    def main():
    s = Solution()
    print s.romanToInt("MCMXCIX")


    if __name__ == "__main__":
    import time
    start = time.clock()
    main()
    print "%s sec" % (time.clock() - start)


  • 相关阅读:
    luogu_1339 [USACO09OCT]热浪Heat Wave
    luogu_1341 无序字母对
    luogu_1330 封锁阳光大学
    luogu_3383 【模板】线性筛素数
    luogu_1095 守望者的逃离
    luogu_1373 小a和uim之大逃离
    查看寄存器
    Assembly oth
    非常详细的端口表汇总
    公式证明
  • 原文地址:https://www.cnblogs.com/Dabay/p/4232841.html
Copyright © 2011-2022 走看看