Compare two version numbers version1 and version2.
If version1> version2 returns 1 if version1 <version2 returns -1, except that 0 is returned.
You can assume that the version string is not empty, and contain numbers and Character.
The character does not represent a decimal point, but used to separate sequence of numbers.
For example, 2.5 is not "two and a half" nor "bad half to three," but the second edition of the fifth small version.
You can assume that the default revision version number of each level is zero. For example, a first stage (large) versions of the 3.4 and the second stage (iteration) Amendment No. 3 and 4, respectively. The third and fourth stages of its revision number are both zero.
Example 1:
Input: version1 = "0.1", version2 = "1.1"
Output: -1
Example 2:
Input: version1 = "1.0.1", version2 = "1"
Output: 1
Example 3:
Input: version1 = "7.5.2.4", version2 = "7.5.3"
Output: -1
Example 4:
Input: version1 = "1.01", version2 = "1.001"
Output: 0
Explanation: ignoring leading zeros, "01" and "001" refer to the same number "a."
Example 5:
Input: version1 = "1.0", version2 = "1.0.0"
Output: 0
to explain: version1 no third-level revision number, which means that its third-level revision number defaults to "0."
prompt:
Version string by a dot (.) Separated numeric strings. This number string may have leading zeros.
Version string does not start or end point, and wherein no two consecutive points.
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/compare-version-numbers
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
Reference blog: https://www.cnblogs.com/grandyang/p/4244123.html
I initially employed using find () and substr () function to solve, and then use atoi () is converted into a digital comparison. The basic solution is consistent with the above blog, but a little tedious point.
//165 int compareVersion(string version1, string version2) { if(version1.empty()) return version2.empty()?0:-1; if(version2.empty()) return version1.empty()?0:1; size_t i=0,j=0; while(i<version1.size() || j<version2.size()) { string str1,str2; while(i<version1.size() && version1[i]!='.') { str1+=version1[i]; i++; } i++; while(j<version2.size() && version2[j]!='.') { str2+=version2[j]; j++; } j++; int num1 = atoi(str1.c_str()); int num2 = atoi(str2.c_str()); if(num1>num2) return -1; if(num1<num2) return -1; } return 0; }//165