TVP

# LeetCode笔记 13.Roman to IntegerEasy

github：

https://github.com/weidafeng/LeetCode_Python

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

Roman numerals are represented by seven different symbols: , , , , , and .

SymbolValueI 1V 5X 10L 50C 100D 500M 1000

For example, two is written as in Roman numeral, just two one's added together. Twelve is written as, , which is simply + . The number twenty seven is written as , which is + + .

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not . Instead, the number four is written as . Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as . There are six instances where subtraction is used:

can be placed before (5) and (10) to make 4 and 9.

can be placed before (50) and (100) to make 40 and 90.

can be placed before (500) and (1000) to make 400 and 900.

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

Example 1:

Input:"III"Output:3

Example 2:

Input:"IV"Output:4

Example 3:

Input:"IX"Output:9

Example 4:

Input:"LVIII"Output:58Explanation:L = 50, V= 5, III = 3.

1、本专栏是我刷LeetCode的学习笔记，每周更新一次

2、所有代码均已上传至github，欢迎star、watch：

https://github.com/weidafeng/LeetCode_Python

3、python语言实现，至少用两种思路，争取用最简洁的代码实现

4、欢迎指正、讨论

• 发表于:
• 原文链接https://kuaibao.qq.com/s/20181219G0L0SX00?refer=cp_1026
• 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。
• 如有侵权，请联系 cloudcommunity@tencent.com 删除。

2023-02-09

2023-02-09

2023-02-09

2018-06-25

2023-02-09

2023-02-09

10元无门槛代金券