It seems a bit of a headache, but it's easier to figure out the rules:
1. The version number is different from the current number, then compare, return-1 or 1
2. The version number is the same as the current number, then the subsequent number is compared. such as 1.1:1 large, but 1.0 and 1 are the same; but 1.0.1 is bigger than 1.
It's not too long to write a good code: D.
Class Solution {Public:int compareversion (string version1, String version2) {while (Version1! = "" | | Version2 = "") {size_t dot1 = Version1.find ('. '), Dot2 = Version2.find ('. '); int level1 = string_to_int (version1.substr (0, DOT1)), int level2 = String_to_int (version2.substr (0, Dot2)); version1 = Dot1! = String::npos? VERSION1.SUBSTR (dot1 + 1): ""; version2 = dot2! = String::npos? VERSION2.SUBSTR (Dot2 + 1): "", if (level1! = Level2) {return level1<level2? -1:1;}} return 0;} private:inline int string_to_int (const string& str) {if (str = = "") {return 0;} int Ret;stringstream SS (str); SS >> ret;return ret;}};
Leetcode 165. Compare Version Numbers