JAVA利用工具类Collections的无返回值sort(List)方法对字符串元素按照自然排序。

字串的连接最长路径查找

http://www.nowcoder.com/questionTerminal/5af18ba2eb45443aa91a11e848aa6723

Collections类(包含大量处理Collection的静态方法)
static <T extends Comparable<? super T>> void sort(List<T> list)
Sorts the specified list into ascending order, according to the natural ordering of its elements.


题目描述

给定n个字符串,请对n个字符串按照字典序排列。

输入描述:

输入第一行为一个正整数n(1≤n≤1000),下面n行为n个字符串(字符串长度≤100),字符串中只含有大小写字母。

输出描述:

数据输出n行,输出结果为按照字典序排列的字符串。

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        while (input.hasNextLine()){
            int num = Integer.parseInt(input.nextLine());
            ArrayList<String> list = new ArrayList<String>();
            while(num > 0){
                list.add(input.nextLine());
                num--;
            }
            Collections.sort(list);
            for (String s : list)
                System.out.println(s);
        }
    }
}


全部评论
import java.util.*; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); while (in.hasNext()) { int n = in.nextInt(); Set<String> set = new TreeSet<>(); while (n>0){ set.add(in.next()); n--; } for (String str : set) { System.out.println(str); } } } } //为什么这个代码只能通过60%呢,可以帮忙解答一下吗
点赞 回复 分享
发布于 2020-02-22 23:29
sorry,set会去重,明白了
点赞 回复 分享
发布于 2020-02-22 23:31

相关推荐

学不完不睡觉11:一眼点评,不过,看运气吧
点赞 评论 收藏
分享
暮雨轻歌:看起来hr不能接受我菜查看图片
点赞 评论 收藏
分享
穿件外套出门:这简历一眼太水了,前面有的没的直接删,写项目亮点
点赞 评论 收藏
分享
评论
点赞
3
分享

创作者周榜

更多
牛客网
牛客企业服务