题解 | 字符串排序

字符串排序

https://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723

import java.util.Scanner;

import java.net.*;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        int num = 0;
        Scanner sc = new Scanner(System.in);
        num = sc.nextInt();
        sc.nextLine();
        TreeSet<String> set = new TreeSet<>((s1, s2) -> {//重定义比较器规则,使其永远不重复
            int result = s1.compareTo(s2);
            return result == 0 ? 1 : result;
        });
        for (int i = 0; i < num; i++) {
            set.add(sc.nextLine());
        }
        for (String string : set) {
            System.out.println(string);
        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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