题解 | #字符串排序#

字符串排序

https://www.nowcoder.com/practice/d9aa3894d3aa4887843a85d26daa4437

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            char[] s = sc.nextLine().toCharArray();
            List<Character> alphas = new ArrayList<>();
            for (char c : s) if (Character.isLetter(c)) alphas.add(c);
            alphas.sort(Comparator.comparingInt(Character::toLowerCase));
            int index = 0;
            for (int i = 0; i < s.length; i++) if (Character.isLetter(s[i])) s[i] = alphas.get(index++);
            System.out.println(s);
        }
    }
}

全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务