My 0ms Java solution


  • 2
    F
    public static int compareVersion(String version1, String version2) {
        int l1 = version1.length();
        int l2 = version2.length();
        int v1 = 0;
        int v2 = 0;
        
        int i1 = 0;
        int i2 = 0;
        
        while (i1 < l1 || i2 < l2){
        	   	
        	while (i1 < l1 && version1.charAt(i1)!='.'){
            	v1 = 10*v1 + (version1.charAt(i1) - '0') ;
            	i1++;
        	}
        	
        	while (i2 < l2 && version2.charAt(i2)!='.'){
            	v2 = 10*v2 + (version2.charAt(i2) - '0') ;
            	i2++;
        	}
            	
            if (v1 > v2) return 1;
            else if (v1 < v2) return -1 ;
            else {
                v1 = 0;
                v2 = 0;
                i1++;
                i2++;
            }
        }
    
        return 0;
    }
    

    "Normally" it runs in only 0ms but sometimes it may run in 1ms (depending on your pc performance), This is a screenshot of my execution time : My 0ms java solution


  • 0
    H

    got the same as you, pretty much exact same solution, 99%


  • 0
    F

    @fujitsu4 idea is better than eclipse


  • 0
    P

    @fujitsu4 If the version number is big enough to cause overflow, then this will not be valid.


  • 0
    L
    good job!

Log in to reply
 

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