Solution


  • 0
    V
    public class Solution {
        public int compareVersion(String version1, String version2) {
            if(!version1.contains(".") && !version2.contains(".")){
    			 Integer ver1 = Integer.parseInt(version1);
    			 Integer ver2 = Integer.parseInt(version2);
    			 if(ver1 > ver2)
    				 return 1;
    			 else if(ver1 < ver2)
    				 return -1;
    			 else
    				 return 0;
    		}
            String[] v1 = version1.split("\\.");
            String[] v2 = version2.split("\\.");
            
            int len1 = v1.length;
            int len2 = v2.length;
            
            int len = Math.min(len1,len2);
            
           
            for(int i = 0 ; i < len ; i++){
                Integer ver1 = Integer.parseInt(v1[i]);
                Integer ver2 = Integer.parseInt(v2[i]);
                if(ver1 > ver2)
                    return 1;
                else if(ver1 < ver2)
                    return -1;
            }
            
            if(len1 > len2){
            	int val = 0;
                for(int i = len2; i < len1 ; i++){
                    Integer ver1 = Integer.parseInt(v1[i]);
                    if(ver1 == 0){
                    	val =  0;
                    }else{
                    	val =  1;
                    }
                }
                return val;
            }
            if(len1 < len2){
            	int val = 0;
                for(int i = len1; i < len2 ; i++){
                    Integer ver2 = Integer.parseInt(v2[i]);
                    if(ver2 == 0){
                        val =  0;
                    }else{
                    	val = -1;
                    }
                }
                return val;
            }
            
            return 0;
        }
    }
    

Log in to reply
 

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