How to translate my CPP solution to python


  • 0
    C

    My cpp solution is:

    class Solution {
    public:
        vector<vector<int> > permuteUnique(vector<int> &num) {
            vector<vector<int> > res;
            vector<int> out;
            vector<int> visited(num.size(), 0);
            sort(num.begin(), num.end());
            permuteUniqueDFS(num, 0, visited, out, res);
            return res;
        }
        void permuteUniqueDFS(vector<int> &num, int level, vector<int> &visited, vector<int> &out, vector<vector<int> > &res) {
            if (level >= num.size()) res.push_back(out);
            else {
                for (int i = 0; i < num.size(); ++i) {
                    if (visited[i] == 0) {
                        if (i > 0 && num[i] == num[i - 1] && visited[i - 1] == 0) continue;
                        visited[i] = 1;
                        out.push_back(num[i]);
                        permuteUniqueDFS(num, level + 1, visited, out, res);
                        out.pop_back();
                        visited[i] = 0;
                    }
                }
            }
        }
    };
    

    I decide to use the python to rewrite it, but because of the python's for loop (python's for .. in .. ), I still cannot solve it. My current python's code is

    class Solution(object):
        def permuteUnique(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
            if not nums:
                return []
            res = []
            curr = []
            judge = [False] * len(nums);
            nums.sort();
            self.DFS(res, curr, nums, judge);
            return res;
        def DFS(self, res, curr, nums, judge):
            if len(curr) == len(nums):
                currcopy = list(curr);
                res.append(currcopy);
                return
            for i in range(0, len(nums)):
                if not judge[i]:
                    judge[i] = True;
                    curr.append(nums[i]);
                    self.DFS(res, curr, nums, judge);
                    judge[i] = False;
                    curr.pop();
                while i< len(nums)-1 and nums[i+1] == nums[i]:
                    i += 1;

  • 0
    C
    class Solution(object):
        def permuteUnique(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
            if not nums:
                return []
            res = []
            curr = []
            judge = [False] * len(nums);
            nums.sort();
            self.DFS(res, curr, nums, judge);
            return res;
        def DFS(self, res, curr, nums, judge):
            if len(curr) == len(nums):
                currcopy = list(curr);
                res.append(currcopy);
                return
            for i in range(0, len(nums)):
                if not judge[i]:
                    if i > 0 and nums[i] == nums[i-1] and judge[i-1] == 0:
                        continue
                    judge[i] = True;
                    curr.append(nums[i]);
                    self.DFS(res, curr, nums, judge);
                    judge[i] = False;
                    curr.pop();
    

    solved :)


Log in to reply
 

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