题解 | #字符串排序#
字符串排序
http://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723
本题刚开始使用的输入方式是Scanner的实例对象,后面提交的结果竟然远低于牛客平均水平,然后我去看了别人的题解,这里看了“工地老哥”的代码(https://www.nowcoder.com/profile/813920409), 发现有很多更快的方法,在这里我学习了他的输入方法,使用BufferedReader来来进行输入,效率提高了不少,内存也降低了不少。
import java.io.*;
import java.util.*;
public class Main{
public void strAsc(String str[]){
for(int i = 0; i < str.length; i++){
for(int j = 0; j < str.length - 1 - i; j++){
if(str[j].compareTo(str[j+1]) > 0){
String tempStr = str[j];
str[j] = str[j + 1];
str[j + 1] = tempStr;
}
}
}
}
public static void main(String []args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
if(1 <= n && n <= 1000){
String [] str = new String [n];
for(int i = 0; i < n; i++){
str[i] = br.readLine();
}
Main test = new Main();
test.strAsc(str);
for(int i = 0; i < str.length; i++){
System.out.println(str[i]);
}
}
}
}