You need to consider:
(1) integer overflow, such as the Numbers for: "99999999999999999999999999999999", cannot be compared to firing a string into an integer;
(2) the first is 0 digital string processing, such as "00000000000000006" is less than the "7",
Written in Java, please sort algorithm implementation and output after sort of string,
The test case:
1, "123 xyz87bbbbg66666666hhhhhh444"
2, "aa999999999999999xyz7777bb0000005"
3, "aaaaaaaa
"4, "100000"
5, "zzzzz100000bbb11ccc899989898989dd220bbbbbbbbbbbbbbbbbb
"6, "zzzzz9999999999999999999999bbb0000000011ccc899989898989dd220bbbbbbbb
"
My idea is like this:
To separate the letters from the string part and digital part, respectively in the two arrays str1, str2, record the position in the string, then the str2 smallest to the number of str1 unchanged, finally in str1 and sorted str2 merge, in the output string,
But the code won't
CodePudding user response:
import Java. Math. BigDecimal;
import java.util.ArrayList;
The import java.util.Com parator;
import java.util.List;
Import the Java. Util. Regex. Matcher;
import java.util.regex.Pattern;
Public class Test07 {
Public static void main (String [] args) {
//TODO Auto - generated method stub
String STR="zzzzz9999999999999999999999bbb0000000011ccc899989898989dd220bbbbbbbb";
System.out.println(str);
String pattern=\ \ "D + | \ \ D +";
The Pattern running r=Pattern.com (Pattern);
The Matcher Matcher=r.m atcher (STR);
ListL1=new ArrayList (a);
ListL2=new ArrayList (a);
While (the matcher. The find ()) {
String temp=the matcher. Group ();
If (l1. The size ()==l2. The size ()
L1. Add (temp);
The else
L2. The add (temp);
}
ComparatorComparator=(x, y) - & gt; New BigDecimal (x). CompareTo (new BigDecimal (y));
If (Character. IsDigit (l1) get (0)) charAt (0)))
L1. Sort (comparator);
The else
L2. Sort (comparator);
String res="";
for(int i=0; I & lt; L1. The size (); I++) {
Res +=l1. Get (I) + (I & lt; L2. The size ()? L2. Get (I) : "");
}
System. The out. Println (res);
}
}