My solution, seems better performance (2 ms)


  • 0
    R
    import java.util.*;
    public class Solution {
        public int compareVersion(String version1, String version2) {
            StringTokenizer v1 = new StringTokenizer(version1, ".");
            StringTokenizer v2 = new StringTokenizer(version2, ".");
            while(v1.hasMoreTokens()){
                String v1Token = v1.nextToken();
                if(v2.hasMoreTokens()){
                    String v2Token = v2.nextToken();
                    int result = Integer.valueOf(v1Token).compareTo(Integer.valueOf(v2Token));
                    if(result !=0){
                        return result;
                    }
                }else{
                    if(Integer.valueOf(v1Token) > 0){
                        return 1;
                    }
                }
            }
            while(v2.hasMoreTokens()){
               String v2Token = v2.nextToken();
                    if(Integer.valueOf(v2Token) > 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.