LeetCode笔记 13.Roman to IntegerEasy

微信公众号:特里斯丹

这里我们有梦

关于文学,关于爱情

关于穿越世界的旅行

github:

https://github.com/weidafeng/LeetCode_Python

这是每周刷LeetCode系列的第4篇笔记

第4周 13. Roman to Integer[Easy]

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
  • 腾讯「云+社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 yunjia_community@tencent.com 删除。

同媒体快讯

扫码关注云+社区

领取腾讯云代金券