Straightforward Java solution


  • 0
    K
    public class Solution {
        public int compareVersion(String version1, String version2) {
            String[] v1 = version1.split("\\.");
            String[] v2 = version2.split("\\.");
            int l1 = v1.length; int l2 = v2.length;
            int d = l1 - l2; int dd = -d;
            while(d > 0){
                version2 += ".0";
                d--;
            }
            while(dd > 0){
                version1 += ".0";
                dd--;
            }
            v1 = version1.split("\\.");
            v2 = version2.split("\\.");
            int len = v1.length;
            for(int i = 0; i < len; i++){
                if(Integer.valueOf(v1[i]) < Integer.valueOf(v2[i])) return -1;
                else if(Integer.valueOf(v1[i]) > Integer.valueOf(v2[i])) return 1;
                else continue;
            }
            return 0;
        }
    }

Log in to reply
 

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