6题解 | #字符串排序#
字符串排序
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); String a = in.nextLine(); char[] cha = a.toCharArray(); List<Character> letters = new LinkedList<>(); for (int i = 0; i < cha.length; i++) { if (Character.isLetter(cha[i])) { letters.add(cha[i]); } } letters.sort(Comparator.comparingInt(o->Character.toLowerCase(o))); StringBuilder sb = new StringBuilder(); for (int i = 0, j = 0; i < cha.length; i++) { if (Character.isLetter(cha[i])) { sb.append(letters.get(j++)); } else { sb.append(cha[i]); } } System.out.println(sb.toString()); } }