示例#1
0
  public void addNum(int val) {
    if (hash.contains(val)) return;
    hash.add(val);

    Interval tmp = new Interval(val, val);

    Interval greater = set.ceiling(tmp);
    Interval smaller = set.floor(tmp);

    if (greater != null) {
      // System.out.println("greater:"+greater.start+" "+greater.end+" val:"+val);
      if (greater.start == val + 1) {
        greater.start = val;
        merge(smaller, greater);

        return;
      }
    }
    if (smaller != null) {
      // System.out.println("smaller:"+smaller.start+" "+smaller.end+" val:"+val);
      if (smaller.end == val - 1) {
        smaller.end = val;
        merge(smaller, greater);
        return;
      }
    }
    set.add(tmp);
    // for(Interval I : set){
    //     System.out.print(I.start+" "+I.end+" ");
    // }
    // System.out.println();
  }
示例#2
0
  /**
   * Returns a shallow copy of this {@code TreeSet} instance. (The elements themselves are not
   * cloned.)
   *
   * @return a shallow copy of this set
   */
  public Object clone() {
    TreeSet<E> clone = null;
    try {
      clone = (TreeSet<E>) super.clone();
    } catch (CloneNotSupportedException e) {
      throw new InternalError();
    }

    clone.m = new TreeMap<E, Object>(m);
    return clone;
  }
示例#3
0
  public boolean isMatch(String s, String p) {
    // without this optimization, it will fail for large data set
    int plenNoStar = 0;
    for (char c : p.toCharArray()) if (c != '*') plenNoStar++;
    if (plenNoStar > s.length()) return false;

    s = " " + s;
    p = " " + p;
    int slen = s.length();
    int plen = p.length();

    boolean[] dp = new boolean[slen]; // 记录0的状态,一共len+1个
    TreeSet<Integer> firstTrueSet = new TreeSet<Integer>();
    firstTrueSet.add(0);
    dp[0] = true;

    boolean allStar = true;
    for (int pi = 1; pi < plen; pi++) {
      if (p.charAt(pi) != '*') allStar = false;
      for (int si = slen - 1; si >= 0; si--) {
        if (si == 0) {
          dp[si] = allStar ? true : false;
        } else if (p.charAt(pi) != '*') {
          if (s.charAt(si) == p.charAt(pi) || p.charAt(pi) == '?') dp[si] = dp[si - 1];
          else dp[si] = false;
        } else {
          int firstTruePos = firstTrueSet.isEmpty() ? Integer.MAX_VALUE : firstTrueSet.first();
          if (si >= firstTruePos) dp[si] = true;
          else dp[si] = false;
        }
        if (dp[si]) firstTrueSet.add(si);
        else firstTrueSet.remove(si);
      }
    }
    return dp[slen - 1];
  }
示例#4
0
 @Override
 public SortedSet<K> keySet() {
   return TreeSet.ofAll(keyComparator(), iterator().map(Tuple2::_1));
 }
示例#5
0
 private void merge(Interval p, Interval n) {
   if (p != null && n != null && (p.end == n.start || p.end + 1 == n.start)) {
     p.end = n.end;
     set.remove(n);
   }
 }