Contains Duplicate III


  • 0

    Click here to see the full article post


  • 0
    J

    For BST solution, the code can still get overflow error if the input case is like
    [2147483646, 2147483647]
    and k = 1, t = 2147483647
    The solution will return false, but it should be true.


  • 0
    A

    The second solution can be shorter, you don't have to check both predecessor and successor, just check if set.ceiling(nums[i]-t) exist and it minus target is small or equal to t. Make sure t is bigger or equal to zero though.


  • 0
    C

    Your first solution is wrong for Input:
    [-1,2147483647]
    1
    2147483647
    Output:
    true
    Expected:
    false


  • 0
    Z

    C++'s std::set, std::set::upper_bound and std::set::lower_bound are NOT equivalent to Java's TreeSet, TreeSet::ceiling and TreeSet::floor


  • 0
    P

    C++'s equivalent of s.ceiling() is s.lower_bound(), but there is no equivalent of s.floor(). To achieve s.floor() you have to use std::next(s.upper_bound(), -1). Note std::set::iterator is a bidirectionalIterator not a randomAccessIterator so you can't use operator-


  • 0
    F

    For the bucket based solution, I don't think converting to long can solve the problem completely - what if we have a test case that max value and t is both Long.MAX_VALUE? It seems like there's a better solution for handling overflow: https://discuss.leetcode.com/topic/80863/10ms-non-cheating-solution-beats-98-84-submissions


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.