LeetCode/string/CompareVersionNumber.java
// BF approach -- memory usage could be improved
BF approach -- memory usage could be improved
BF approach -- memory usage could be improved
public int compareVersionBF(String version1, String version2) {
        if (version1.isEmpty() || version2.isEmpty()) {
            return 0;
        }
        version1 = version1.substring(12, version1.length() - 1);
        version2 = version2.substring(12, version2.length() - 1);
        ArrayList<String> v1Token = new ArrayList<>(Arrays.asList(version1.split("\\.")));
        ArrayList<String> v2Token = new ArrayList<>(Arrays.asList(version2.split("\\.")));
        while (v2Token.size() < v1Token.size()) {
            v2Token.add("0");
        }
        while (v1Token.size() < v2Token.size()) {
            v1Token.add("0");
        }
        for (int i = 0; i < v1Token.size(); i++) {
            if (Integer.parseInt(v1Token.get(i)) > Integer.parseInt(v2Token.get(i))) {
                return 1;
            } else if (Integer.parseInt(v1Token.get(i)) < Integer.parseInt(v2Token.get(i))) {
                return -1;
            }
        }
        return 0;
    }
public int compareVersionBF ( String version1 , String version2 ) { if ( version1 . isEmpty ( ) || version2 . isEmpty ( ) ) { return 0 ; } version1 = version1 . substring ( 12 , version1 . length ( ) - 1 ) ; version2 = version2 . substring ( 12 , version2 . length ( ) - 1 ) ; ArrayList < String > v1Token = new ArrayList < > ( Arrays . asList ( version1 . split ( "\\." ) ) ) ; ArrayList < String > v2Token = new ArrayList < > ( Arrays . asList ( version2 . split ( "\\." ) ) ) ; while ( v2Token . size ( ) < v1Token . size ( ) ) { v2Token . add ( "0" ) ; } while ( v1Token . size ( ) < v2Token . size ( ) ) { v1Token . add ( "0" ) ; } for ( int i = 0 ; i < v1Token . size ( ) ; i ++ ) { if ( Integer . parseInt ( v1Token . get ( i ) ) > Integer . parseInt ( v2Token . get ( i ) ) ) { return 1 ; } else if ( Integer . parseInt ( v1Token . get ( i ) ) < Integer . parseInt ( v2Token . get ( i ) ) ) { return - 1 ; } } return 0 ; }
BF approach -- memory usage could be improved
BF approach -- memory usage could be improved
// clean the input --> only have pure version string // THIS part is not needed // separated by . array // fill in the length (0) // compare from higher to lower