求第k大

寻找第K大

http://www.nowcoder.com/questionTerminal/e016ad9b7f0b45048c58a9f27ba618bf

import java.util.*;
import java.util.stream.Collectors;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String str = sc.nextLine();
String replace = str.replace("[", "").replace("]", "");
String[] split = replace.split(",");
Integer n = null;
Integer k = null;
Set<integer> set = new HashSet<>();
for (int i = 0; i < split.length; i++) {
if (i == (split.length - 1)) {
k = Integer.parseInt(split[i]);
}
if (i == (split.length - 2)) {
n = Integer.parseInt(split[i]);
}
set.add(Integer.parseInt(split[i]));
}
List<integer> collect = set.stream().sorted(Comparator.comparing(Integer::intValue).reversed()).collect(Collectors.toList());
System.out.println(collect.get(k - 1));
}</integer></integer>

}

}

全部评论

相关推荐

Natrium_:这时间我以为飞机票
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务