题解 | #比较版本号#
比较版本号
https://www.nowcoder.com/practice/2b317e02f14247a49ffdbdba315459e7
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 比较版本号 * @param version1 string字符串 * @param version2 string字符串 * @return int整型 */ int compare(char* version1, char* version2 ) { // write code here int l1 = strlen(version1), l2 = strlen(version2); int i = 0, j = 0; while(i < l1 || j < l2) { char tmp1[1002], tmp2[1002]; int i1 = 0, j1 = 0; long long temp1 = 0, temp2 = 0; while(i < l1 && version1[i] != '.') { while(i < l1 && version1[i] == '0') { i++; } if(version1[i] == '.' || i == l1) { tmp1[i1++] = '0'; break; } tmp1[i1++] = version1[i++]; printf("i1 is %d tmp1[i1] is %c\n", i1 - 1, tmp1[i1 - 1]); } for(int k = i1 - 1; k >= 0; k--) { temp1 = temp1 * 10 + (tmp1[k] - '0'); } printf("temp1 : %lld\n", temp1); while(j < l2 && version2[j] != '.') { while(j < l2 && version2[j] == '0') j++; if(version2[j] == '.' || j == l2) { tmp2[j1++] = '0'; break; } tmp2[j1++] = version2[j++]; printf("j1 is %d tmp2[j1] is %c\n", j1 - 1, tmp2[j1 - 1]); } for(int k = j1 - 1; k >= 0; k--) { temp2 = temp2 * 10 + (tmp2[k] - '0'); } printf("temp2 : %lld\n", temp2); if(temp1 > temp2) return 1; else if(temp1 < temp2) return -1; else { i++; j++; } } return 0; }