<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>roman Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/roman/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/roman/</link>
	<description></description>
	<lastBuildDate>Wed, 15 Jan 2020 04:16:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>roman Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/roman/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 13. Roman to Integer</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-13-roman-to-integer/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-13-roman-to-integer/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 19 Sep 2018 15:39:39 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[roman]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4036</guid>

					<description><![CDATA[<p>Problem Roman numerals are represented by seven different symbols:&#160;I,&#160;V,&#160;X,&#160;L,&#160;C,&#160;D&#160;and&#160;M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-13-roman-to-integer/">花花酱 LeetCode 13. Roman to Integer</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 13. Roman to Integer - 刷题找工作 EP299" width="500" height="375" src="https://www.youtube.com/embed/joFpSO_sHnU?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>


<h1><strong>Problem</strong></h1>
<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>,&nbsp;<code>V</code>,&nbsp;<code>X</code>,&nbsp;<code>L</code>,&nbsp;<code>C</code>,&nbsp;<code>D</code>&nbsp;and&nbsp;<code>M</code>.</p>
<pre class="crayon:false"><strong>Symbol</strong>       <strong>Value</strong>
I             1
V             5
X             10
L             50
C             100
D             500
M             1000</pre>
<p>For example,&nbsp;two is written as&nbsp;<code>II</code>&nbsp;in Roman numeral, just two one&#8217;s added together. Twelve is written as,&nbsp;<code>XII</code>, which is simply&nbsp;<code>X</code>&nbsp;+&nbsp;<code>II</code>. The number twenty seven is written as&nbsp;<code>XXVII</code>, which is&nbsp;<code>XX</code>&nbsp;+&nbsp;<code>V</code>&nbsp;+&nbsp;<code>II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not&nbsp;<code>IIII</code>. Instead, the number four is written as&nbsp;<code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as&nbsp;<code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code>&nbsp;can be placed before&nbsp;<code>V</code>&nbsp;(5) and&nbsp;<code>X</code>&nbsp;(10) to make 4 and 9.</li>
<li><code>X</code>&nbsp;can be placed before&nbsp;<code>L</code>&nbsp;(50) and&nbsp;<code>C</code>&nbsp;(100) to make 40 and 90.</li>
<li><code>C</code>&nbsp;can be placed before&nbsp;<code>D</code>&nbsp;(500) and&nbsp;<code>M</code>&nbsp;(1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input:</strong>&nbsp;"III"
<strong>Output:</strong> 3</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input:</strong>&nbsp;"IV"
<strong>Output:</strong> 4</pre>
<p><strong>Example 3:</strong></p>
<pre class="crayon:false"><strong>Input:</strong>&nbsp;"IX"
<strong>Output:</strong> 9</pre>
<p><strong>Example 4:</strong></p>
<pre class="crayon:false"><strong>Input:</strong>&nbsp;"LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> C = 100, L = 50, XXX = 30 and III = 3.
</pre>
<p><strong>Example 5:</strong></p>
<pre class="crayon:false"><strong>Input:</strong>&nbsp;"MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.</pre>
<h1><strong>Solution</strong></h1>
<p>accumulate the value of each letter.</p>
<p>If the value of current letter is greater than the previous one, deduct twice of the previous value.</p>
<p>e.g. IX, 1 + 10 &#8211; 2 * 1 = 9 instead of 1 + 10 = 11</p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p>
<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int romanToInt(string s) {
    map&lt;char, int&gt; m{{'I', 1}, {'V', 5}, 
                     {'X', 10}, {'L', 50},
                     {'C', 100}, {'D', 500},
                     {'M', 1000}};
    int ans = 0;
    for (int i = 0; i &lt; s.length(); i++) {
      ans += m[s[i]];
      if (i &gt; 0 &amp;&amp; m[s[i]] &gt; m[s[i - 1]])
        ans -= 2 * m[s[i - 1]];
    }
    return ans;
  }
};</pre>

</div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
  private int v(char R) {
    switch (R) {
      case 'I' : return 1;
      case 'V' : return 5;
      case 'X' : return 10;
      case 'L' : return 50;
      case 'C' : return 100;
      case 'D' : return 500;
      case 'M' : return 1000;
      default: break;
    }
    return 0;
  }
  
  public int romanToInt(String s) {
    int ans = 0;
    for (int i = 0; i &lt; s.length(); i++) {
      ans += v(s.charAt(i));
      if ( i &gt; 0 &amp;&amp; v(s.charAt(i)) &gt; v(s.charAt(i - 1)))        
          ans -= 2 * v(s.charAt(i - 1));        
    }
    return ans;
  }
}</pre>

</div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution:
  def romanToInt(self, s):
    m = {'I' : 1, 'V': 5, 'X': 10, 'L' : 50,
         'C' : 100, 'D': 500, 'M': 1000}
    ans = m[s[0]]
    for c, p in zip(s[1:], s[0:-1]):
      ans += m[c]
      if m[c] &gt; m[p]: ans -= 2 * m[p]
    return ans</pre>
</div></div><p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-13-roman-to-integer/">花花酱 LeetCode 13. Roman to Integer</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/string/leetcode-13-roman-to-integer/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 12. Integer to Roman</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-12-integer-to-roman/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-12-integer-to-roman/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 13 Sep 2018 03:18:07 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[roman]]></category>
		<category><![CDATA[simulation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3940</guid>

					<description><![CDATA[<p>Problem Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-12-integer-to-roman/">花花酱 LeetCode 12. Integer to Roman</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre class="crayon:false"><strong>Symbol</strong>       <strong>Value</strong>
I             1
V             5
X             10
L             50
C             100
D             500
M             1000</pre>
<p>For example, two is written as <code>II</code> in Roman numeral, just two one&#8217;s added together. Twelve is written as, <code>XII</code>, which is simply <code>X</code> + <code>II</code>. The number twenty seven is written as <code>XXVII</code>, which is <code>XX</code> + <code>V</code> + <code>II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9.</li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90.</li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input:</strong> 3
<strong>Output:</strong> "III"</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input:</strong> 4
<strong>Output:</strong> "IV"</pre>
<p><strong>Example 3:</strong></p>
<pre class="crayon:false"><strong>Input:</strong> 9
<strong>Output:</strong> "IX"</pre>
<p><strong>Example 4:</strong></p>
<pre class="crayon:false"><strong>Input:</strong> 58
<strong>Output:</strong> "LVIII"
<strong>Explanation:</strong> C = 100, L = 50, XXX = 30 and III = 3.
</pre>
<p><strong>Example 5:</strong></p>
<pre class="crayon:false"><strong>Input:</strong> 1994
<strong>Output:</strong> "MCMXCIV"
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.</pre>
<h1><strong>Solution: HashTable + Simulation</strong></h1>
<p>Map integer 1,4,5,9,10,40,50,90, &#8230;, 1000 to Romain</p>
<p>Start from the largest number y,</p>
<p>if x &gt;= y:<br />
ans += Roman[y]<br />
x -= y</p>
<p>Time complexity: O(x)</p>
<p>Space complexity: O(x)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  string intToRoman(int num) {
    vector&lt;pair&lt;int,string&gt;&gt; v{{1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"},
                               { 100, "C"}, { 90, "XC"}, { 50, "L"}, { 40, "XL"}, 
                               {  10, "X"}, {  9, "IX"}, {  5, "V"}, {  4, "IV"}, 
                               {   1, "I"}};
    string ans;
    auto it = cbegin(v);
    while (num) {
      if (num &gt;= it-&gt;first) {
        num -= it-&gt;first;
        ans += it-&gt;second;
      } else {
        ++it;
      }
    }
    return ans;
  }
};</pre><p></div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def intToRoman(self, num):
    m = [[1000, "M"], [900, "CM"], [500, "D"], [400, "CD"],
         [ 100, "C"], [ 90, "XC"], [ 50, "L"], [ 40, "XL"], 
         [  10, "X"], [  9, "IX"], [  5, "V"], [  4, "IV"], 
         [   1, "I"]]    
    index = 0
    ans = ''
    while num &gt; 0:
      if num &gt;= m[index][0]:
        ans += m[index][1]
        num -= m[index][0]
      else:
        index += 1
    return ans</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-12-integer-to-roman/">花花酱 LeetCode 12. Integer to Roman</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/simulation/leetcode-12-integer-to-roman/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
