题解 | #字符串排序#

字符串排序

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

Character 排序 array.sort(Comparator.compareInt(Character::toLowerCase));

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){
            
            String str = scan.nextLine();
            List<Character> list = new ArrayList<>();
            StringBuilder strb = new StringBuilder();
            for(char c : str.toCharArray()){
                if(Character.isLetter(c)){
                    list.add(c);
                }
            }
            list.sort(Comparator.comparingInt(Character::toLowerCase));
            for(int i = 0, j = 0; i<str.length(); i++){
                if(Character.isLetter(str.charAt(i))){
                    strb.append(list.get(j));
                    j++;
                }
                else{
                    strb.append(str.charAt(i));
                }
            }
            System.out.println(strb.toString());
        }
        scan.close();
    }
}








全部评论

相关推荐

qq乃乃好喝到咩噗茶:院校后面加上211标签,放大加粗,招呼语也写上211
点赞 评论 收藏
分享
头顶尖尖的程序员:我是26届的不太懂,25届不应该是找的正式工作吗?为什么还在找实习?大四还实习的话是为了能转正的的岗位吗
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 11:47
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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