rambless
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String str = in.nextLine();
char[] arr = str.toCharArray();
char[] tar = new char[arr.length];
List<Character> list = new ArrayList<>();
for(int i = 0; i<arr.length; i++) {
if((arr[i]>=97 && arr[i]<=122) || (arr[i]>=65 && arr[i]<=90)) {
list.add(arr[i]);
} else {
tar[i] = arr[i];
}
}
Queue<Character> queue = dealWord(list, str);
for(int i = 0; i<arr.length; i++) {
if(tar[i] == 0) {
System.out.print(queue.poll());
} else {
System.out.print(arr[i]);
}
}
}
}
private static Queue<Character> dealWord(List<Character> list, String str) {
Queue<Character> queue = new LinkedList<>();
int[] big = new int[26];
int[] sma = new int[26];
Arrays.fill(big, 0);
Arrays.fill(sma, 0);
for(int i = 0; i<list.size(); i++) {
if(list.get(i)>=97 && list.get(i)<=122) {
//小写
sma[list.get(i)-97]++;
} else {
//大写
big[list.get(i)-65]++;
}
}
for(int i = 0; i<26; i++) {
if(big[i] != 0 && sma[i] != 0) {
//大小写同时存在,找出大小写的顺序
List<Integer> sort = new ArrayList<>();
for(int j = 0; j<list.size(); j++) {
if(list.get(j) == (i+65)) {
sort.add(0);
}
if(list.get(j) == (i+97)) {
sort.add(1);
}
}
for(int m=0; m<sort.size(); m++) {
if(sort.get(m) == 0) {
queue.offer((char)(i+65));
} else {
queue.offer((char)(i+97));
}
}
} else if(big[i] != 0) {
for(int j=0; j<big[i]; j++) {
queue.offer((char)(i+65));
}
} else if(sma[i] != 0) {
for(int j=0; j<sma[i]; j++) {
queue.offer((char)(i+97));
}
} else {
//都没有
}
}
return queue;
}
}

