<?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>dot Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/dot/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/dot/</link>
	<description></description>
	<lastBuildDate>Mon, 09 Mar 2020 08:06:59 +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>dot Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/dot/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 165. Compare Version Numbers</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-165-compare-version-numbers/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-165-compare-version-numbers/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 09 Mar 2020 08:06:39 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[dot]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6435</guid>

					<description><![CDATA[<p>Compare two version numbers&#160;version1&#160;and&#160;version2.If&#160;version1&#160;&#62;&#160;version2&#160;return&#160;1;&#160;if&#160;version1&#160;&#60;&#160;version2&#160;return&#160;-1;otherwise return&#160;0. You may assume that the version strings are non-empty and contain only digits and the&#160;.&#160;character. The&#160;.&#160;character does not represent a&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-165-compare-version-numbers/">花花酱 LeetCode 165. Compare Version Numbers</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Compare two version numbers&nbsp;<em>version1</em>&nbsp;and&nbsp;<em>version2</em>.<br>If&nbsp;<code><em>version1</em>&nbsp;&gt;&nbsp;<em>version2</em></code>&nbsp;return&nbsp;<code>1;</code>&nbsp;if&nbsp;<code><em>version1</em>&nbsp;&lt;&nbsp;<em>version2</em></code>&nbsp;return&nbsp;<code>-1;</code>otherwise return&nbsp;<code>0</code>.</p>



<p>You may assume that the version strings are non-empty and contain only digits and the&nbsp;<code>.</code>&nbsp;character.</p>



<p>The&nbsp;<code>.</code>&nbsp;character does not represent a decimal point and is used to separate number sequences.</p>



<p>For instance,&nbsp;<code>2.5</code>&nbsp;is not &#8220;two and a half&#8221; or &#8220;half way to version three&#8221;, it is the fifth second-level revision of the second first-level revision.</p>



<p>You may assume the default revision number for each level of a version number to be&nbsp;<code>0</code>. For example, version number&nbsp;<code>3.4</code>&nbsp;has a revision number of&nbsp;<code>3</code>&nbsp;and&nbsp;<code>4</code>&nbsp;for its first and second level revision number. Its third and fourth level revision number are both&nbsp;<code>0</code>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> <code><em>version1</em></code> = "0.1", <code><em>version2</em></code> = "1.1"
<strong>Output:</strong> -1</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input: </strong><code><em>version1</em></code> = "1.0.1", <code><em>version2</em></code> = "1"
<strong>Output:</strong> 1</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> <code><em>version1</em></code> = "7.5.2.4", <code><em>version2</em></code> = "7.5.3"
<strong>Output:</strong> -1</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> <code><em>version1</em></code> = "1.01", <code><em>version2</em></code> = "1.001"
<strong>Output:</strong> 0
<strong>Explanation:</strong> Ignoring leading zeroes, both “01” and “001" represent the same number “1”</pre>



<p><strong>Example 5:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> <code><em>version1</em></code> = "1.0", <code><em>version2</em></code> = "1.0.0"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The first version number does not have a third level revision number, which means its third level revision number is default to "0"</pre>



<p><strong>Note:</strong></p>



<ol><li>Version strings are composed of numeric strings separated by dots&nbsp;<code>.</code>&nbsp;and this numeric strings&nbsp;<strong>may</strong>&nbsp;have leading zeroes.</li><li>Version strings do not start or end with dots, and they will not be two consecutive dots.</li></ol>



<p>Solution: String</p>



<p>Split the version string to a list of numbers, and compare two lists.</p>



<p>Time complexity: O(l1 + l2)<br>Space complexity: O(l1 + l2)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int compareVersion(string version1, string version2) {
    const auto&amp; v1 = parseVersion(version1);
    const auto&amp; v2 = parseVersion(version2);
    int i1 = 0, i2 = 0;
    while (i1 &lt; v1.size() || i2 &lt; v2.size()) {    
      int n1 = i1 &lt; v1.size() ? v1[i1++] : 0;
      int n2 = i2 &lt; v2.size() ? v2[i2++] : 0;
      if (n1 &lt; n2) return -1;
      else if (n1 &gt; n2) return 1;
    }
    return 0;
  }
private:
  vector&lt;int&gt; parseVersion(const string&amp; version) {
    vector&lt;int&gt; v;
    int s = 0;
    for (char c : version) {
      if (c == '.') {
        v.push_back(s);
        s = 0;
      } else {
        s = s * 10 + (c - '0');
      }
    }
    v.push_back(s);
    return v;
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-165-compare-version-numbers/">花花酱 LeetCode 165. Compare Version Numbers</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-165-compare-version-numbers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
