美文网首页
LeetCode#13 Roman to Integer

LeetCode#13 Roman to Integer

作者: 如烟花非花 | 来源:发表于2016-11-18 11:08 被阅读15次

问题描述

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

补充说明:

这个题目的要求很简单,给定你一个罗马字符串,给出它的阿拉伯数字表示。

方案分析

  1. 什么是罗马数字?

    维基百科说明 [1]: https://en.wikipedia.org/wiki/Roman_numerals "Roman numerals"

    简要的说明一下,

<table class="wikitable">
<tr>
<th>Symbol</th>
<td><span class="times-serif" title="Roman numeral">I</span></td>
<td><span class="times-serif" title="Roman numeral">V</span></td>
<td><span class="times-serif" title="Roman numeral">X</span></td>
<td><span class="times-serif" title="Roman numeral">L</span></td>
<td><span class="times-serif" title="Roman numeral">C</span></td>
<td><span class="times-serif" title="Roman numeral">D</span></td>
<td><span class="times-serif" title="Roman numeral">M</span></td>
</tr>
<tr>
<th>Value</th>
<td>1</td>
<td>5</td>
<td>10</td>
<td>50</td>
<td>100</td>
<td>500</td>
<td>1,000</td>
</tr>
</table>

上面表格给出的是常用的罗马字符对应的阿拉伯数值。

<strong>要注意的来了:</strong>


<table class="wikitable">
<tbody><tr>
<th>Number</th>
<td>4</td>
<td>9</td>
<td>40</td>
<td>90</td>
<td>400</td>
<td>900</td>
</tr>
<tr>
<th>Notation</th>
<td><span class="times-serif" title="Roman numeral">IV</span></td>
<td><span class="times-serif" title="Roman numeral">IX</span></td>
<td><span class="times-serif" title="Roman numeral">XL</span></td>
<td><span class="times-serif" title="Roman numeral">XC</span></td>
<td><span class="times-serif" title="Roman numeral">CD</span></td>
<td><span class="times-serif" title="Roman numeral">CM</span></td>
</tr>
</tbody></table>

  • <span class="times-serif" title="Roman numeral">I</span> placed before <span class="times-serif" title="Roman numeral">V</span> or <span class="times-serif" title="Roman numeral">X</span> indicates one less, so four is <span class="times-serif" title="Roman numeral">IV</span> (one less than five) and nine is <span class="times-serif" title="Roman numeral">IX</span> (one less than ten)
  • <span class="times-serif" title="Roman numeral">X</span> placed before <span class="times-serif" title="Roman numeral">L</span> or <span class="times-serif" title="Roman numeral">C</span> indicates ten less, so forty is <span class="times-serif" title="Roman numeral">XL</span> (ten less than fifty) and ninety is <span class="times-serif" title="Roman numeral">XC</span> (ten less than a hundred)
  • <span class="times-serif" title="Roman numeral">C</span> placed before <span class="times-serif" title="Roman numeral">D</span> or <span class="times-serif" title="Roman numeral">M</span> indicates a hundred less, so four hundred is <span class="times-serif" title="Roman numeral">CD</span> (a hundred less than five hundred) and nine hundred is <span class="times-serif" title="Roman numeral">CM</span> (a hundred less than a thousand)<sup id="cite_ref-sun_5-0" class="reference">[5]

    简单翻译一下,就是:

    当I出现在V和X前面的时候,相当于减去1;

    当X出现在L和C前面的时候,相当与减去10;

    当C出现在D和M前面的时候,相当于减去100;
  1. 前面那段是简介,基本要传递出来的就是两点:

    (1)哪个罗马字符对应哪个阿拉伯值。

    (2)一句话给你概括了,就是当后面的那个罗马数字比前面罗马数字大的话,相当于减去后面那个罗马数字的值。

python实现

class Solution:
  """
  :type s: str
  :rtype: int
  """
  def romanToInt(self, s):
      roman = {'M': 1000,'D': 500 ,'C': 100,'L': 50,'X': 10,'V': 5,'I': 1}
      z = 0
      for i in range(0, len(s) - 1):
          if roman[s[i]] < roman[s[i+1]]:
              z -= roman[s[i]]
          else:
              z += roman[s[i]]
      return z + roman[s[-1]]

相关文章

网友评论

      本文标题:LeetCode#13 Roman to Integer

      本文链接:https://www.haomeiwen.com/subject/enijpttx.html