1ms Java without split...


  • 0
    F
    public   int compareVersion(String version1, String version2) {
    	char[] v1 = version1.toCharArray();
    	char[] v2 = version2.toCharArray();
    	int n1 = 0, n2 = 0;
    	int p1 = 0, p2 = 0;
    	while (true) {
    		while (p1 < v1.length && v1[p1] != '.') {
    			if (v1[p1] == '0' && n1 == 0) {
    				p1++;
    				continue;
    			}
    			n1 += (v1[p1++] - '0');
    			n1 *= 10;
    		}
    		while (p2 < v2.length && v2[p2] != '.') {
    			if (v2[p2] == '0' && n2 == 0) {
    				p2++;
    				continue;
    			}
    			n2 += (v2[p2++] - '0');
    			n2 *= 10;
    		}
    	//	System.out.println("compare " + n1 + " " + n2);
    		if (n1 == n2) {
    			if (p1 >= v1.length && p2 >= v2.length) {
    				return 0;
    			}
    			n1 = 0;
    			n2 = 0;
    			p1++;
    			p2++;
    			continue;
    
    		}
    		return n1 < n2 ? -1 : 1;
    	}
    }

Log in to reply
 

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