题解 | #比较版本号#
比较版本号
https://www.nowcoder.com/practice/2b317e02f14247a49ffdbdba315459e7
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 比较版本号
* @param version1 string字符串
* @param version2 string字符串
* @return int整型
*/
int chartoint(char a){
return a-'0';
}
int compare(string version1, string version2) {
// write code here
int i=0,j=0,res=0;
int l1 =version1.length(),l2=version2.length();
while (i<l1||j<l2) {
int v1=0,v2=0;
while(i<l1 && version1[i]!='.')v1=10*v1+chartoint(version1[i++]) ;
while(j<l2 && version2[j]!='.')v2=10*v2+chartoint(version2[j++]) ;
if(v1>v2)return 1;
else if (v1<v2) return -1;
i++,j++;
}
return 0;
}
};