微信公众号:特里斯丹
这里我们有梦
关于文学,关于爱情
关于穿越世界的旅行
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、欢迎指正、讨论
如果帮到你了
欢迎点赞+分享
感谢
领取专属 10元无门槛券
私享最新 技术干货