Home > Back-end >  To a string containing only lowercase letters and Numbers of the positive integer number sequence ac
To a string containing only lowercase letters and Numbers of the positive integer number sequence ac

Time:04-24

To a string containing only lowercase letters and Numbers of the positive integer number sequence according to the Numbers in ascending order size, the relative position of the rest of the character remains the same, only change in the original number of substring, such as string as "aaa77xxxx88jj44", after the sorting result is "aaa44xxxx77jj88 non-numeric substring" aaa ", "XXXX" and "jj" relative position remains unchanged,
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);
List L1=new ArrayList (a);
List L2=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);
}

Comparator Comparator=(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);
}

}
  • Related