字符串排序, 华为

import java.util.*;
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            char[] a = sc.nextLine().toCharArray();
            List<Integer[]> list = new ArrayList<>();
            int idx = 0;
            for(int i=0; i < a.length; i++) {
                if((a[i] >= 'a' && a[i] <= 'z') || (a[i] >= 'A' && a[i] <= 'Z')) {
                    list.add(new Integer[] {(int)a[i], idx++});
                }
            }
            Collections.sort(list, new Comparator<Integer[]>(){
                public int compare(Integer[] o1, Integer[] o2) {
                    int x = o1[0] >= 'a' ? o1[0] -32 : o1[0];
                    int y = o2[0] >= 'a' ? o2[0] -32 : o2[0];
                    return x == y ? o1[1] - o2[1] : x - y;
                }
            });
            idx = 0;
            for(int i=0; i < a.length; i++) {
                if((a[i] >= 'a' && a[i] <= 'z') || (a[i] >= 'A' && a[i] <= 'Z')) {
                    int t = list.get(idx++)[0];
                    a[i] = (char) t;
                }
            }
            System.out.println(String.valueOf(a));
        }
    }
}
posted @ 2020-07-10 18:40  li修远  阅读(143)  评论(0编辑  收藏  举报