0
点赞
收藏
分享

微信扫一扫

笔试算法《字符串排序》


题目

  • 题目描述
  • 给定n个字符串,请对n个字符串按照字典序排列。
  • 输入描述:
    输入第一行为一个正整数n(1≤n≤1000),下面n行为n个字符串(字符串长度≤100),字符串中只含有大小写字母。
  • 输出描述:
    数据输出n行,输出结果为按照字典序排列的字符串。
  • 示例1
  • 输入
    9
    cap
    to
    cat
    card
    two
    too
    up
    boat
    boot
  • 输出
    boat
    boot
    cap
    card
    cat
    to
    too
    two
    up

代码

package org.lht.boot.lang.suanfa;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

/**
* @author haitao.li
* @description: 题目描述
* 给定n个字符串,请对n个字符串按照字典序排列。
* 输入描述:
* 输入第一行为一个正整数n(1≤n≤1000),下面n行为n个字符串(字符串长度≤100),字符串中只含有大小写字母。
* 输出描述:
* 数据输出n行,输出结果为按照字典序排列的字符串。
* 示例1
* 输入
* <p>
* 9
* cap
* to
* cat
* card
* two
* too
* up
* boat
* boot
* 输出
* <p>
* boat
* boot
* cap
* card
* cat
* to
* too
* two
* up
* @date 2021/4/14 10:36
*/
public class Huawei字符串排序 {

/**
* 利用Arrays.sort方法排序,在牛客网有人跑出7ms,不知道怎么跑的
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(reader.readLine());
String[] strings = new String[n];
while (n > 0) {
strings[n - 1] = reader.readLine();
n--;
}
Arrays.sort(strings);
for (int i = 0; i < strings.length; i++) {
System.out.println(strings[i]);
}
}
}

笔试算法《字符串排序》_java


举报

相关推荐

0 条评论