0.1 < 1.1 return -1; // like Comparable Class's Comparator's public int compare method, ascending -1
1.2 > 1.1 return 1;// descending , 1
13.37 == 13.37 return 0
2. Implementation
Q1: How to compare if there is a decimal point?
A1: split into two parts from decimal point, then compare the first part if first part is equal, proceed to second part if we have second part
Input:"1", "1.1"
Output:0
Expected:-1
One is len 2 and the other is len 1
which will not compare the second one if len is not balanced
Input:"1.0", "1"
Output:1
Expected:0
Runtime Error Message:Line 102: java.lang.ArrayIndexOutOfBoundsException: 1
Last executed input:"1", "1.0"
Input:"10.6.5", "10.6"
Output:0
Expected:1
// NOTE: dot is a special char in regexes, So you must escape it with a leading backslash, But the leading backslash is a special character, So it must be escaped too, with another leading backslash
Output:0
Expected:1
// NOTE: dot is a special char in regexes, So you must escape it with a leading backslash, But the leading backslash is a special character, So it must be escaped too, with another leading backslash
It means "match all chars except newlines"
String[] v1Arr = v1.split("\\.");
String[] v2Arr = v2.split("\\.");
// NOTE: may have more than one decimal point
// NOTE: may have more than one decimal point
while ( index < v1Arr.length || index < v2Arr.length )
{
// NOTE: same length
// NOTE: same length
if ( index < v1Arr.length && index < v2Arr.length)
{
// NOTE: v1 longer
// NOTE: v1 longer
else if ( index < v1Arr.length )
{
if ( Integer.parseInt(v1Arr[index]) != 0 )
return 1; }
// NOTE: v2 longer
else if ( index < v2Arr.length )
{
if ( Integer.parseInt(v2Arr[index]) != 0 )
return -1; }
// time:O(length of splitted arry), Space:O(n) array public int compareVerison(String v1, String v2) { // validate the input if ( v1== null && v2 == null ) return 0; if ( v1== null || v1.length()==0 ) return -1; if ( v2 ==null || v2.length() == 0) return 1; // http://docs.oracle.com/javase/7/docs/api/java/lang/String.html v1.trim(); v2.trim(); // NOTE: Line 75: error: incompatible types: char cannot be converted to String //String[] v1Arr = v1.split('.'); //String[] v2Arr = v2.split('.'); //String[] v1Arr = v1.split("."); //String[] v2Arr = v2.split("."); //http://stackoverflow.com/questions/13460595/using-string-split-with-a-decimal-not-working // NOTE: dot is a special char in regexes(It means "match all chars except newlines"), So you must escape it with a leading backslash, But the leading backslash is a special character, So it must be escaped too, with another leading backslash String[] v1Arr = v1.split("\\."); String[] v2Arr = v2.split("\\."); // You may assume that the version strings are non-empty and contain only digits and the . character. // No need to worry no '.' int index = 0; //while ( index < v2Arr.length && index < v1Arr.length ) // NOTE: may have more than one decimal point while ( index < v1Arr.length || index < v2Arr.length ) { // NOTE:http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html //int v1Value = Integer.valuesOf( v1Arr[index]); //int v2Value = Integer.valuesOf( v2Arr[index]); //int v1Value = Integer.valueOf( v1Arr[index]); //int v2Value = Integer.valueOf( v2Arr[index]); //if ( v1Value < v2Value ) // return -1; //else if ( v1Value > v2Value ) // return 1; //else //{ // if (index ==1) // return 0; //} // NOTE: same length if ( index < v1Arr.length && index < v2Arr.length) { if ( Integer.parseInt(v1Arr[index]) < Interger.parseInt(v2Arr[index]) ) return -1; else if ( Integer.parseInt(v1Arr[index]) > Integer.parseInt(v2Arr[index]) ) return 1; } // NOTE: v1 longer else if ( index < v1Arr.length ) { if ( Integer.parseInt(v1Arr[index]) != 0 ) return 1; } // NOTE: v2 longer else if ( index < v2Arr.length ) { if ( Integer.parseInt(v2Arr[index]) != 0 ) return -1; } index++; } return 0; }3. Similar ones
Comparable Class Comparator
No comments:
Post a Comment