K小
public class KthSmallElement {
static int minSize;
public static void findKthSmall(List li_data, int th) {
PriorityQueue<Integer> queue = new PriorityQueue<Integer>( 11,new Mycompare());
if(th<=0)
System.out.println(0);
minSize = th;
for(int i =0;i<li_data.size();i++){
if(queue.size()<minSize)
queue.offer((Integer) li_data.get(i));
else{
int top = queue.peek();
System.out.println(top);
if(top> (Integer)li_data.get(i)){
queue.poll();
queue.offer((Integer) li_data.get(i));
}
}
}
System.out.println(queue.peek());
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
List<Integer> li_data=new ArrayList<Integer>();
while(in.hasNext()){
li_data.add(in.nextInt());
}
int th=li_data.remove(li_data.size()-1);
findKthSmall(li_data,th);
}
}
class Mycompare implements Comparator<Integer>{
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
}
K大,AC 了的
public class KthLargestElement {
static int maxSize;
public static void findKthLargest(List li_data, int th) {
PriorityQueue<Integer> queue = new PriorityQueue<Integer>();
if(th<=0)
System.out.println(0);
maxSize = th;
for(int i =0;i<li_data.size();i++){
if(queue.size()<maxSize)
queue.offer((Integer) li_data.get(i));
else{
int top = queue.peek();
System.out.println(top);
if(top< (Integer)li_data.get(i)){
queue.poll();
queue.offer((Integer) li_data.get(i));
}
}
}
System.out.println(queue.peek());
System.out.println(queue);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
List<Integer> li_data=new ArrayList<Integer>();
while(in.hasNext()){
li_data.add(in.nextInt());
}
int th=li_data.remove(li_data.size()-1);
findKthLargest(li_data,th);
}
}