165. Compare Version Numbers
来源: LeetCode 165. Compare Version Numbers
题目描述
165. Compare Version Numbers
Compare two version numbers version1 and version2.
If version1 > version2 return 1; if version1 < version2 return -1;otherwise return 0.
You may assume that the version strings are non-empty and contain only digits and the . character.
The . character does not represent a decimal point and is used to separate number sequences.
For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.
You may assume the default revision number for each level of a version number to be 0. For example, version number 3.4 has a revision number of 3 and 4 for its first and second level revision number. Its third and fourth level revision number are both 0.
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 zeroes, both “01” and “001" represent the same number “1”
Example 5:
Input: version1 = "1.0", version2 = "1.0.0"
Output: 0
Explanation: The first version number does not have a third level revision number, which means its third level revision number is default to "0"
Note:
Version strings are composed of numeric strings separated by dots . and this numeric strings may have leading zeroes.
Version strings do not start or end with dots, and they will not be two consecutive dots.
思路分析
如何去除前导0
当一个字符串判断结束后有两种情况另外一个字符串如果后续均为0相等否则先结束的版本小
依次判断每个中间的序列号
尽量不要用stoi如果中间字符超过位数一定会error
代码
class Solution {
#define SIZE(A) ((int)A.size())
#define LENGTH(A) ((int)A.length())
#define MP(A,B) make_pair(A,B)
#define PB(X) push_back(X)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,a) for(int i=0;i<(a);++i)
#define ALL(A) A.begin(),A.end()
using VI = vector<int>;
using VII = vector<VI>;
using VD = vector<double>;
typedef pair<int, int> PI;
public:
int compareVersion(string version1, string version2) {
int is=0, id=0, js=0, jd=0;
while(id<SIZE(version1) && jd<SIZE(version2)){
is = id; js = jd;
while(id<SIZE(version1) && version1[id] != '.') ++id;
while(jd<SIZE(version2) && version2[jd] != '.') ++jd;
while(version1[is] == '0') ++is;
while(version2[js] == '0') ++js; // remove leading zero
if((is == id) ^ (js == jd)) return is == id ? -1 : 1; // one zero on bigger than zero
if(jd - js != id - is) return jd + is > id + js ? -1 : 1;
while(is<id && js<jd){
if(version1[is] > version2[js]) return 1;
else if(version1[is] < version2[js]) return -1;
++is;
++js;
}
++id; ++jd; // 下一串
}
if(is == SIZE(version1) && id == SIZE(version2)) return 0;
return is == SIZE(version1) ? isZero(version2.substr(js, SIZE(version2)-js)) ? 0 : -1 : isZero(version1.substr(is, size(version1)-is)) ? 0 : 1; // one over if the other is zero then return 0 else return bigger (1,-1)
}
bool isZero(string s){
REP(i, SIZE(s)){
if(s[i] == '0' || s[i] == '.') continue;
else return false;
}
return true;
}
};
算法分析
- 时间复杂度:O(max(m,n)
- 空间复杂度:O(1)
代码改进