My JAVA solution without split


  • 51
    L
    public class Solution {
    public int compareVersion(String version1, String version2) {
        int temp1 = 0,temp2 = 0;
        int len1 = version1.length(),len2 = version2.length();
        int i = 0,j = 0;
        while(i<len1 || j<len2) {
            temp1 = 0;
            temp2 = 0;
            while(i<len1 && version1.charAt(i) != '.') {
                temp1 = temp1*10 + version1.charAt(i++)-'0';
                
            }
            while(j<len2 && version2.charAt(j) != '.') {
                temp2 = temp2*10 + version2.charAt(j++)-'0';
                
            }
            if(temp1>temp2) return 1;
            else if(temp1<temp2) return -1;
            else {
                i++;
                j++;
                
            }
            
        }
        return 0;
        
    }
    

    }


  • 1
    W

    I'm a little confused. What about 2.23 and 2.3? What is the result?


  • -1
    M

    It should yield 1, because the first digit of 2.23 is 2 and the first digit of 2.3 is 3.


  • 2
    G

    23 > 3, so the result is 1


  • 0
    Z

    I like this solution than other Java solutions.


  • 0
    C

    Best solution ever!


Log in to reply
 

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