Failed with input "cdd" but get correct answer in my own compiler


  • 0
    J

    Here is my code:

    #include <vector>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Solution {
    public:
    	vector<vector<string> > ret;
    	vector<vector<int> > path;
    
    	vector<vector<string> > partition(string s) {
    		path.assign(s.length(), vector<int>(s.length(), 0));
    		for (int i = 1; i <= s.length(); ++ i) {
    			for (int j = 0; j <= s.length() - i; ++ j) {
    				if (s[j] == s[j+i-1]) {
    					if (i <= 3 || path[j+1][j+i-2]) {
    						path[j][j+i] = 1;
    					}
    				}
    			}
    		}
    		dfs(s, 0, vector<string>());
    		return ret;
    	}
    	void dfs(string s, int pos, vector<string> ans) {
    		if (pos == s.length()) {
    			ret.push_back(ans);
    		} else {
    			for (int i = pos + 1; i <= s.length(); ++ i) {
    				if (path[pos][i]) {
    					vector<string> v(ans);
    					v.push_back(s.substr(pos, i - pos));
    					dfs(s, i, v);
    				}
    			}
    		}
    	}
    };
    
    int main() {
    	Solution sol = Solution();
    	vector<vector<string> > ret = sol.partition("cdd");
    	for (vector<vector<string> >::iterator i = ret.begin(); i != ret.end(); ++ i) {
    		vector<string> ans = *i;
    		for (vector<string>::iterator j = ans.begin(); j != ans.end(); ++ j) {
    			cout << *j << "#";
    		}
    		cout << endl;
    	}
    	return 0;
    }

  • 0
    J

    Leetcode gived a run-time-error, but my compiler gives the correct result (I am using g++-4.9)


Log in to reply
 

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