Code has Run time error on OJ (works on local machine)


  • 0
    M

    //I got run time error with input "1111" on OJ. I tried the same code on my windows machine with //VS2013 and it works. Thanks a lot for any suggestions.

        class Solution { 
            public:
            	vector<string> restoreIpAddresses(string s) {
            		string t;
            		backtracing(s, 0, t, 0);
            		return temp;
            	}
            	void backtracing(const string &s, int level, string t, int offset)
            	{
            		if (level == 4)
            			return;
            		string::iterator iter;
            		string::const_iterator iter1;
            		iter = t.begin() + offset + level;
            		iter1 = s.begin() + offset;
            		for (int i = 1; i<4 && i + offset <= s.size(); i++)
            		{
            			if (STN(s, offset, i) <= 255 && (s[offset] != '0' || (s[offset] == '0' && i == 1)))
            			{
            				t.replace(iter, iter + (i - 1), iter1, iter1 + i);
            				if (t.size()>0)
            				if (*(t.end() - 1) == '.')
            					t.erase(t.end() - 1);
            				if (level == 3 && t.size() == s.size() + 3)
            				{
            					temp.push_back(t);
            				}
            				else
            				{
            					t += '.';
            					backtracing(s, level + 1, t, offset + i);
            				}
            			}
            		}
            	}
            	int STN(const string &s, int offset, int i) //string to integer
            	{
            		int r = 0;
            		int t;
            		for (int k = offset; k<offset + i; k++)
            		{
            			t = s[k] - '0';
            			r = r * 10 + t;
            		}
            		return r;
            	}
            	vector<string> temp;
            };

Log in to reply
 

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