Java Code which beats 99% runtime!


  • 5
    A
    public class Solution {
        public int compareVersion(String version1, String version2) {
            char[] chars1 = version1.toCharArray();
            char[] chars2 = version2.toCharArray();
            int m = chars1.length;
            int n = chars2.length;
            int i = 0;
            int j = 0;
            int num1 = 0;
            int num2 = 0;
            while (i < m || j < n) {
                while (i < m && chars1[i] != '.') {
                    num1 = 10 * num1 + chars1[i] - '0';
                    ++i;
                }
                while (j < n && chars2[j] != '.') {
                    num2 = 10 * num2 + chars2[j] - '0';
                    ++j;
                }
                if (num1 > num2) {
                    return 1;
                }
                if (num1 < num2) {
                    return -1;
                }
                num1 = 0;
                num2 = 0;
                ++i;
                ++j;
            }
            return 0;
        }
    }

  • 0
    A

    @A1546488968 This is a nice solution. Thanks for posting. Could be made concise though. Thanks.


Log in to reply
 

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