Java Solution Easy to Understand Compare Version


#1
public class Solution {
    public int compareVersion(String A, String B) {
        String[] first = A.split("\\.");
        String[] second = B.split("\\.");
        int p1=0;
        int p2=0;
        while(p1<first.length&&p2<second.length){
            try{
                long left = Long.parseLong(first[p1]);
                long right = Long.parseLong(second[p2]);
                if(left!=right){
                    return left>right?1:-1;
                }
                p1++;
                p2++;
            } catch(Exception e){
                int cp = first[p1].compareTo(second[p2]);
                if(cp!=0){
                    return cp>0?1:-1;
                }
                p1++;
                p2++;
            }
        }

        while(p1<first.length&&first[p1].equals("0")){
            p1++;
        }
        
        if(p1<first.length){
            return 1;
        }

        while(p2<second.length&&second[p2].equals("0")){
            p2++;
        }
        if(p2<second.length){
            return -1;
        }

        return 0;
    }
}