(LeetCode) Roman to Integer --- 罗马数字转整数
2016-10-12 10:04
507 查看
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
Subscribe to see which companies asked this question
解题分析:
首先要了解,罗马数字对应的整数:
然后要了解罗马数字的排序方式,从左到右依次从大到小。
# -*- coding:utf-8 -*-
__author__ = 'jiuzhang'
class Solution(object):
def romanToInt(self, s):
digits = { "I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000 }
len_s = len(s)
num = 0
for i in range(0, len_s - 1):
cur = digits[s[i]]
next = digits[s[i + 1]]
if cur >= next:
num += cur
else:
num -= cur
num += digits[s[len_s - 1]]
return num
Input is guaranteed to be within the range from 1 to 3999.
Subscribe to see which companies asked this question
解题分析:
首先要了解,罗马数字对应的整数:
"I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000
然后要了解罗马数字的排序方式,从左到右依次从大到小。
# -*- coding:utf-8 -*-
__author__ = 'jiuzhang'
class Solution(object):
def romanToInt(self, s):
digits = { "I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000 }
len_s = len(s)
num = 0
for i in range(0, len_s - 1):
cur = digits[s[i]]
next = digits[s[i + 1]]
if cur >= next:
num += cur
else:
num -= cur
num += digits[s[len_s - 1]]
return num
相关文章推荐
- leetcode-12. Integer to Roman(整数转换成罗马数字)
- [LeetCode] Roman to Integer 罗马数字转化成整数
- [LeetCode] Roman to Integer 罗马数字转整数
- 【LeetCode-面试算法经典-Java实现】【013-Roman to Integer (罗马数字转成整数)】
- [LeetCode]12. Integer to Roman(整数转化为罗马数字)
- [Leetcode] Roman to integer 罗马数字转成整数
- Leetcode 13 Roman to Integer 罗马数字转整数
- LeetCode 12 Integer to Roman (整数转罗马数字)
- [LeetCode] Integer to Roman 整数转化成罗马数字
- LeetCode--Roman to Integer 罗马数字转化成整数
- [C++]LeetCode 12: Integer to Roman(将整数转换为罗马数字)
- LeetCode | Integer to Roman(整数转换成罗马数字)
- leetcode:Integer to Roman(整数转化为罗马数字)
- leetCode 12.Integer to Roman (整数转罗马数字) 解题思路和方法
- Leet Code 13 Roman to Integer - 罗马数字转成整数 - Java
- Roman to Integer 罗马数字转化为整数
- [Leetcode] Interger to roman 整数转成罗马数字
- LeetCode OJ 之 Integer to Roman (整数转化为罗马数字)
- Leetcode 12 Integer to Roman整数变罗马
- [LintCode] Roman to Integer 罗马数字转化成整数