Time limit exceed


  • -1
    T
    #include <vector>
    using namespace std;
    
    #include <string>
    using namespace std;
    
    #include <cstring>
    using namespace std;
    
    #include <cstdlib>
    using namespace std;
    
    class Solution {
    private:
        vector<int> stringtonumber(string num)
        {
            vector<int> value;
            for(int i=0; i<num.length(); i++)
            {
                string sub = num.substr(i, 1);
                value.push_back(atoi(sub.c_str()));
            }
            return value;
        }
        
    public:
        vector<string> addOperators(string num, int target) {
            vector<string> final;
            if(num.length() == 0)
                return final;
            vector<int> value = stringtonumber(num);
            int calculated;
            int count = 1;
            int i;
            for(i=0; i<value.size()-1; i++)
                count = count*4;
            for(i=0; i<count; i++)
            {
                int HH = i;
                int temp;
                vector<int> tempval;
                vector<char> opers;
                int j;
                int hello;
                int yes = 1;
                tempval.push_back(value[0]);
                if(value.size() > 1)
                {
                for(j=1; j<=value.size()-1;j++)
                {
                    temp = HH%4;
                    if(temp == 0)
                    {
                        hello = tempval.back();
                        if(hello == 0)
                            yes = 0;
                        tempval.pop_back();
                        hello = hello*10+value[j];
                        tempval.push_back(hello);
                    }
                    
                    else if(temp==1)
                    {
                        tempval.push_back(value[j]);
                        opers.push_back('*');
                    }
                    
                    else if(temp==2)
                    {
                        tempval.push_back(value[j]);
                        opers.push_back('+');
                    }
                    
                    else
                    {
                        tempval.push_back(value[j]);
                        opers.push_back('-');
                    }
                    HH = HH/4;
                }
                }
                vector<int> tempvalue;
                vector<char> operators;
                
                tempvalue.push_back(tempval[0]);
                if(opers.size() > 0)
                {
                    for(j=0; j<=opers.size()-1; j++)
                    {
                        if(opers[j] == '*')
                        {
                            hello = tempvalue.back();
                            tempvalue.pop_back();
                            hello = hello*tempval[j+1];
                            tempvalue.push_back(hello);
                        }
                        else if(opers[j] == '+')
                        {
                            tempvalue.push_back(tempval[j+1]);
                            operators.push_back('+');
                        }
                        else
                        {
                            tempvalue.push_back(tempval[j+1]);
                            operators.push_back('-');
                        }
                    }
                }
                calculated = tempvalue[0];
                if(operators.size() > 0)
                {
                    for(j=0; j<=operators.size()-1; j++)
                    {
                        if(operators[j] == '+')
                            calculated += tempvalue[j+1];
                        else
                            calculated -= tempvalue[j+1];
                    }
                }
    
               if(calculated == target && yes == 1)
                {
                    string s = to_string(value[0]);
                    HH = i;
                    for(j=1; j<=value.size()-1; j++)
                    {
                        temp = HH%4;
                        if(temp==0)
                            s = s + to_string(value[j]);
                        else if(temp==1)
                            s = s + "*" + to_string(value[j]);
                        else if(temp==2)
                            s = s + "+" + to_string(value[j]);
                        else
                            s = s + "-" + to_string(value[j]);
                        HH = HH/4;
                    }
                    final.push_back(s);
                }
            }
            return final;
        }
    };

Log in to reply
 

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