My recursive java solution


  • 0
    F
    public int compareVersion(String version1, String version2) {
    	if (version1.equals(version2))
    		return 0;
    
    	int index1 = version1.indexOf(".");
    	int ver1 = version1.indexOf(".") > 0 ? Integer.valueOf(version1
    			.substring(0, index1)) : Integer.valueOf(version1);
    
    	int index2 = version2.indexOf(".");
    	int ver2 = version2.indexOf(".") > 0 ? Integer.valueOf(version2
    			.substring(0, index2)) : Integer.valueOf(version2);
    
    	if (ver1 > ver2)
    		return 1;
    	if (ver1 < ver2)
    		return -1;
    
    	String rest1 = index1 > 0 ? version1.substring(index1 + 1) : "0";
    	String rest2 = index2 > 0 ? version2.substring(index2 + 1) : "0";
    	return compareVersion(rest1, rest2);        
    }

Log in to reply
 

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