Time Limit Exceeded


  • 1
    A
    class Solution {
    public:
        vector<vector<int> > fourSum(vector<int> &num, int target) {
            int i,j,k,t,temp,l,r,sum,n=num.size(),x,y,ans;
            vector<vector<int>>v;
            sort(num.begin(),num.end());
            vector<int>a;
            for(i=0;i<n-3;)
            {
                j=i+1;
                while(j<n-2)
                {
                    t=num[j];
                    sum=num[i]+t;
                    target=target-sum;
                    l=j+1;
                    r=n-1;
                    while(l<r)
                    {
                        ans=num[l]+num[r];
                        if(ans==target)
                        {
                            a.push_back(num[i]);
                            a.push_back(num[j]);
                            a.push_back(num[l]);
                            a.push_back(num[r]);
                            v.push_back(a);
                            a.clear();
                            x=num[l];
                            y=num[r];
                            while(x==num[l])
                            l++;
                            while(y==num[r])
                            r--;
                        }
                    }
                    while(t==num[j])
                    j++;
                }
                temp=num[i];
                while(temp==num[i])
                i++;
            }
            return v;
        }
    };

  • 0
    S

    Could you please update your post with the explanation of your algorithm and add comment in your code? Or people barely help on the raw code. Thanks.


  • 1

    In your code,
    if(ans!=target) nothing changed, so infinite loop
    also i think you need to store the original target somewhere

    And Shangrila really always give helpless comments and good at removing others' comments


  • 0

    clydexu@ I have directed Shangrila as the moderator here. The rules in Discuss are clearly stated in the FAQ. If we notice someone did not follow the rule, we might remove the post/comment to maintain the quality of Discuss.

    Here, I think Shangrila is doing the right thing by commenting on the question to let the original poster know that the question should be revised. Shangrila is being nice to allow new poster a second chance for a revision. Besides, Shangrila has contributed many quality answers to Discuss (even more than you and me combined), how can this be helpless?


  • 0

    I appreciate all the staffs contributing to the web site.

    But I saw neat codes warned for not adding comments many times, and I know some codes without any comments already been removed.

    Of course you can comment your opinions but should not be only you.

    People did not post any stupid comments, but the neat codes. The rules do ask for explanation, but I think neat codes already explain themselves very well. Different people have different standards. Many people including me can learn a lot from those smart guys, but those guys may never post again.

    I appreciate the efforts done by Shangrila a lot, but he is also shutting people up.

    And I don't believe anything is more important than encouraging people to share their minds.


Log in to reply
 

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