首页 > 编程语言 > 详细

字符串排序, 华为

时间:2020-07-10 20:31:16      阅读:67      评论:0      收藏:0      [点我收藏+]
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));
        }
    }
}

字符串排序, 华为

原文:https://www.cnblogs.com/lixyuan/p/13280760.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!