How to improve this solution?


  • 0
    G

    Below is my solution and I think it's not so good both in code style and efficiency. But it took me quite long time to get it accepted : (. How can I improve it?

    class Solution {
    public:
        vector<vector<int> > result;
        int MAX;
        bool *chosen;
        vector<int> oneRes;
        vector<int> *given;
        vector<vector<int> > permute(vector<int> &num) {
            MAX = num.size();
            given = &num;
            chosen = new bool[MAX];
            for(int i = 0; i < MAX; i++){
                chosen[i] = false;
            }
            perm(0);
            return result;
        }
        void perm(int idx){
        	if(idx >= MAX){
    			vector<int> temp = oneRes;
        		result.push_back(temp);
        		return;
        	}
        	for(int i = 0; i < MAX; i++){
        		if(chosen[i] == true){
        			continue;
        		}
        		oneRes.push_back((*given)[i]);
        		chosen[i] = true;
        		perm(idx + 1);
    			oneRes.pop_back();
        		chosen[i] = false;
        	}
        }
    };

Log in to reply
 

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