java的treeset为什么WA了呀
查看原帖
java的treeset为什么WA了呀
607834
yangCode01楼主2022/3/12 13:03
import java.util.Scanner;
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        TreeSet<Integer> ts = new TreeSet<>();
        int n = sc.nextInt();
        while (n-- > 0) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            if (a == 1) {//存
                if (!ts.add(b))//是否重复存放
                    System.out.println("Already Exist");
            } else {//取
                if (ts.isEmpty())//如果空,直接不执行下面
                    System.out.println("Empty");
                else {
                    if (ts.contains(b)) {//取到了且合适
                        System.out.println(b);
                        ts.remove(b);
                    } else {//没找到合适的,取比他小的且接近的
                        if (ts.lower(b) != null) {//看有没有
                            int val = ts.lower(b);
                            System.out.println(val);
                            ts.remove(val);
                        } else {//取比他大的且接近的
                            if (ts.higher(b) != null) {//看有没有
                                int val = ts.higher(b);
                                System.out.println(val);
                                ts.remove(val);
                            }
                        }
                    }
                }
            }
        }
    }
}

2022/3/12 13:03
加载中...