My simple Java solution


  • 3
    H
    public int compareVersion(String v1, String v2) {
        
        String[] va = v1.split("\\.");
        String[] vb = v2.split("\\.");
    
        for(int i=0, j=0; i<va.length||j<vb.length; i++,j++){
            int a = i<va.length ? Integer.valueOf(va[i]) : 0;
            int b = j<vb.length ? Integer.valueOf(vb[j]) : 0;
            if(a>b) return 1;
            if(a<b) return -1;
        }
        return 0;
    }

Log in to reply
 

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