牛客华为机试HJ14
作者:互联网
1. 问题描述
2. Solution
1、思路分析
逐个读入word,放到PriorityQueue中,读入结束后,逐个出队输出即可。
2、代码实现
Java实现
import java.io.IOException;
import java.nio.file.Paths;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main {
static Scanner in;
public static void main(String[] args) throws IOException {
if (!"Linux".equals(System.getProperty("os.name"))) {
in = new Scanner(Paths.get("/Users/jun/Documents/Learn/JavaLearning/NowCoder/src/huawei/HJ014/input.txt"));
} else {
in = new Scanner(System.in);
}
while (in.hasNext()) {
int n = in.nextInt();
solve(n);
}
}
private static void solve(int n) {
PriorityQueue<String> queue = new PriorityQueue<>();
for (int i = 0; i < n; i++) {
String cur = in.next();
queue.add(cur);
}
while (!queue.isEmpty()) {
System.out.println(queue.poll());
}
}
}
Python
import sys
if sys.platform != "linux":
sys.stdin = open("input/HJ14.txt")
n = int(input().strip())
words = [line.strip() for line in sys.stdin]
words.sort()
for x in words:
print(x)
3、复杂度分析
时间复杂度: O(n)
空间复杂度: O(n)
标签:queue,java,HJ14,int,PriorityQueue,牛客,机试,import,Scanner 来源: https://www.cnblogs.com/junstat/p/16070062.html