My Java solution


  • 2
    S
    public class Solution {
    public int compareVersion(String version1, String version2) {
        if (version1 == null || version2 == null) return 0;
        String[] vr1 = version1.split("\\.");
        String[] vr2 = version2.split("\\.");
        int l1 = vr1.length;
        int l2 = vr2.length;
        int len = l1 >= l2 ? l1 : l2;
        int v1, v2;
        for (int i = 0; i < len; i++) {
        	v1 = (i >= l1 ? 0 : Integer.parseInt(vr1[i]));
        	v2 = (i >= l2 ? 0 : Integer.parseInt(vr2[i]));
        	if (v1 > v2) return 1;
        	else if (v1 < v2) return -1;
        }
        return 0;
    }

Log in to reply
 

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