My simple java solution


  • 0
    X
    public class Solution {
        public int compareVersion(String version1, String version2) {
            String[] v1=version1.split("\\.");
            String[] v2=version2.split("\\.");
            
            int length1=v1.length;
            int length2=v2.length;
            
            if(length1==0 && length2!=0) return -1;
            if(length1!=0 && length2==0) return 1;
            if(length1==0 && length2==0) return 0;
            
            int i=0;
            for(;i<Math.min(length1,length2);i++){
                if(Integer.parseInt(v1[i])<Integer.parseInt(v2[i])) return -1;
                else if(Integer.parseInt(v1[i])>Integer.parseInt(v2[i])) return 1;
                else continue;
            }
            
            if(i<length2 && Integer.parseInt(v2[i])!=0)  return -1;
            if(i<length1 && Integer.parseInt(v1[i])!=0) return 1;
            
            return 0;
        }
    }

Log in to reply
 

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