{"id":6435,"date":"2020-03-09T01:06:39","date_gmt":"2020-03-09T08:06:39","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6435"},"modified":"2020-03-09T01:06:59","modified_gmt":"2020-03-09T08:06:59","slug":"leetcode-165-compare-version-numbers","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-165-compare-version-numbers\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 165. Compare Version Numbers"},"content":{"rendered":"\n<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>\n\n\n\n<p>You may assume that the version strings are non-empty and contain only digits and the&nbsp;<code>.<\/code>&nbsp;character.<\/p>\n\n\n\n<p>The&nbsp;<code>.<\/code>&nbsp;character does not represent a decimal point and is used to separate number sequences.<\/p>\n\n\n\n<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>\n\n\n\n<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>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> <code><em>version1<\/em><\/code> = \"0.1\", <code><em>version2<\/em><\/code> = \"1.1\"\n<strong>Output:<\/strong> -1<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input: <\/strong><code><em>version1<\/em><\/code> = \"1.0.1\", <code><em>version2<\/em><\/code> = \"1\"\n<strong>Output:<\/strong> 1<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<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\"\n<strong>Output:<\/strong> -1<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> <code><em>version1<\/em><\/code> = \"1.01\", <code><em>version2<\/em><\/code> = \"1.001\"\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> Ignoring leading zeroes, both \u201c01\u201d and \u201c001\" represent the same number \u201c1\u201d<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<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\"\n<strong>Output:<\/strong> 0\n<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>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><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>\n\n\n\n<p>Solution: String<\/p>\n\n\n\n<p>Split the version string to a list of numbers, and compare two lists.<\/p>\n\n\n\n<p>Time complexity: O(l1 + l2)<br>Space complexity: O(l1 + l2)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int compareVersion(string version1, string version2) {\n    const auto& v1 = parseVersion(version1);\n    const auto& v2 = parseVersion(version2);\n    int i1 = 0, i2 = 0;\n    while (i1 < v1.size() || i2 < v2.size()) {    \n      int n1 = i1 < v1.size() ? v1[i1++] : 0;\n      int n2 = i2 < v2.size() ? v2[i2++] : 0;\n      if (n1 < n2) return -1;\n      else if (n1 > n2) return 1;\n    }\n    return 0;\n  }\nprivate:\n  vector<int> parseVersion(const string& version) {\n    vector<int> v;\n    int s = 0;\n    for (char c : version) {\n      if (c == '.') {\n        v.push_back(s);\n        s = 0;\n      } else {\n        s = s * 10 + (c - '0');\n      }\n    }\n    v.push_back(s);\n    return v;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Compare two version numbers&nbsp;version1&nbsp;and&nbsp;version2.If&nbsp;version1&nbsp;&gt;&nbsp;version2&nbsp;return&nbsp;1;&nbsp;if&nbsp;version1&nbsp;&lt;&nbsp;version2&nbsp;return&nbsp;-1;otherwise return&nbsp;0. You may assume that the version strings are non-empty and contain only digits and the&nbsp;.&nbsp;character. The&nbsp;.&nbsp;character does not represent a&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[93,567,177,4],"class_list":["post-6435","post","type-post","status-publish","format-standard","hentry","category-string","tag-conversion","tag-dot","tag-medium","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6435","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6435"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6435\/revisions"}],"predecessor-version":[{"id":6437,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6435\/revisions\/6437"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}