TLE doubts for this question

  • 0

    public class TwoSum {
    ArrayList<Integer> list;

    /** Initialize your data structure here. */
    public TwoSum() {
        list = new ArrayList<Integer>();    
    /** Add the number to an internal data structure.. */
    public void add(int number) {
    /** Find if there exists any pair of numbers which sum is equal to the value. */
    public boolean find(int value) {
        HashSet<Integer> set = new HashSet<Integer>();
        for(int i = 0; i < list.size(); i++) {
            if(set.contains(value - list.get(i))) {
                return true;
        return false;


    This is a very easy solution for this question. I believe there's no point to optimize. Unfortunately, OJ shows TLE. I don't think my solution is slower than the other solutions in the forum. Can anyone tell me why my solution is unacceptable?

Log in to reply

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