Click here to see the full article post
For BST solution, the code can still get overflow error if the input case is like
and k = 1, t = 2147483647
The solution will return false, but it should be true.
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.
Your first solution is wrong for Input:
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
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-
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
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.