题解 | #字符串排序#

字符串排序

http://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584

import java.util.*;
public class Main {
    static Scanner in = new Scanner(System.in);
    public static void main(String[] args) {
        String str = in.nextLine();
        ArrayList<Character> list = new ArrayList<>();
        for (char c: str.toCharArray()) {
            if (Character.isLetter(c)) 
                list.add(c);
        }
        list.sort((a, b)->Character.toLowerCase(a) - Character.toLowerCase(b));
        int idx = 0;
        for (char c : str.toCharArray()) {
            if (Character.isLetter(c)) {
                System.out.print(list.get(idx ++ ));
            } else {
                System.out.print(c);
            }
        }
    }
}
全部评论

相关推荐

挥毫自在:想白嫖你呢
点赞 评论 收藏
分享
评论
点赞
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务