My Java Solution


  • 0
    J
     public class Solution {
        public int compareVersion(String version1, String version2) {
            String[] s1 = version1.split("\\.");
            String[] s2 = version2.split("\\.");
            int i = 0, j = 0, m = s1.length, n = s2.length;
            while (i < m && j < n) {
                int d1 = Integer.parseInt(s1[i]), d2 = Integer.parseInt(s2[j]);
                if (d1 > d2) return 1;
                if (d1 < d2) return -1;
                i++;
                j++;
            }
            for(; i < m; i++){
                if (Integer.parseInt(s1[i]) > 0) return 1;
            }
            for(; j < n; j++){
                if (Integer.parseInt(s2[j]) > 0) return -1;
            }
            return 0;
        }
    }
    

    This handles the case where version1 = "1.0.0.1", version2 = "1.0".


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.