题解 | #字符串排序#
字符串排序
http://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
参照:https://blog.csdn.net/qq_35812205/article/details/114049721 进行了Java版本的实现。
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNextLine()){
String str = input.nextLine();
char[] chars = str.toCharArray();
//英文26个字母,每个字母(大小写)一个链表
LinkedList<Character>[] vertex = new LinkedList[26];
for (int i = 0; i < 26; i++) {
vertex[i] = new LinkedList<>();
}
for (int i = 0; i < str.length(); i++) {
if (chars[i]>='A'&&chars[i]<='Z'){
vertex[chars[i]-'A'].addLast(chars[i]);
}
if (chars[i]>='a'&&chars[i]<='z'){
vertex[chars[i]-'a'].addLast(chars[i]);
}
}
//把26个字母链表整合成一个字母链表
LinkedList<Character> letters = new LinkedList<>();
for (int i = 0 ;i < 26;i++){
while(!vertex[i].isEmpty()) {
char chr = vertex[i].removeFirst();
letters.addLast(chr);
}
}
//字母链表和输入字符串进行整理合并
for (int i = 0; i < chars.length; i++) {
if ((chars[i]>='a'&&chars[i]<='z')||(chars[i] >='A'&&chars[i]<='Z')){
chars[i] = letters.removeFirst();
}
}
System.out.println(chars);
}
}
}