Backtrack Summary: General Solution for 10 Questions!!!!!!!! Python (Combination Sum, Subsets, Permutation,  Palindrome)
For Java version, please refer to isssac3's answer.\n\n39. Combination Sum\nhttps://leetcode.com/problems/combination-sum/\n\n    def combinationSum(self, candi
For Java version, please refer to [isssac3's answer.](https://discuss.leetcode.com/topic/46162/a-general-approach-to-backtracking-questions-in-java-subsets-permutations-combination-sum-palindrome-partioning)\n\n**39. Combination Sum**\nhttps://leetcode.com/problems/combination-sum/\n```\n    def combinationSum(self, candidates, target):\n        def backtrack(tmp, start, end, target):\n            if target == 0:\n                ans.append(tmp[:])\n            elif target > 0:\n                for i in range(start, end):\n                    tmp.append(candidates[i])\n                    backtrack(tmp, i, end, target - candidates[i])\n                    tmp.pop()\n        ans = [] \n        candidates.sort(reverse= True)\n        backtrack([], 0, len(candidates), target)\n        return ans\n```\n\n**40. Combination Sum II**\nhttps://leetcode.com/problems/combination-sum-ii/\n```\n    def combinationSum2(self, candidates, target):\n        def backtrack(start, end, tmp, target):\n            if target == 0:\n                ans.append(tmp[:])\n            elif target > 0:\n                for i in range(start, end):\n                    if i > start and candidates[i] == candidates[i-1]:\n                        continue\n                    tmp.append(candidates[i])\n                    backtrack(i+1, end, tmp, target - candidates[i])\n                    tmp.pop()\n        ans = []\n        candidates.sort(reverse= True)\n        backtrack(0, len(candidates), [], target)\n        return ans\n```\n\n**78. Subsets**\nhttps://leetcode.com/problems/subsets/\n```\n    def subsets(self, nums):\n        def backtrack(start, end, tmp):\n            ans.append(tmp[:])\n            for i in range(start, end):\n                tmp.append(nums[i])\n                backtrack(i+1, end, tmp)\n                tmp.pop()\n        ans = []\n        backtrack(0, len(nums), [])\n        return ans\n```\n\n**90. Subsets II**\nhttps://leetcode.com/problems/subsets-ii/\n```\n    def subsetsWithDup(self, nums):\n        def backtrack(start, end, tmp):\n            ans.append(tmp[:])\n            for i in range(start, end):\n                if i > start and nums[i] == nums[i-1]:\n                    continue\n                tmp.append(nums[i])\n                backtrack(i+1, end, tmp)\n                tmp.pop()\n        ans = []\n        nums.sort()\n        backtrack(0, len(nums), [])\n        return ans\n```\n\n**46. Permutations**\nhttps://leetcode.com/problems/permutations/\n```\n    def permute(self, nums):\n        def backtrack(start, end):\n            if start == end:\n                ans.append(nums[:])\n            for i in range(start, end):\n                nums[start], nums[i] = nums[i], nums[start]\n                backtrack(start+1, end)\n                nums[start], nums[i] = nums[i], nums[start]\n                \n        ans = []\n        backtrack(0, len(nums))\n        return ans\n```\n\n**47. Permutations II**\nhttps://leetcode.com/problems/permutations-ii/\n```\n    def permuteUnique(self, nums):\n        def backtrack(tmp, size):\n            if len(tmp) == size:\n                ans.append(tmp[:])\n            else:\n                for i in range(size):\n                    if visited[i] or (i > 0 and nums[i-1] == nums[i] and not visited[i-1]):\n                        continue\n                    visited[i] = True\n                    tmp.append(nums[i])\n                    backtrack(tmp, size)\n                    tmp.pop()\n                    visited[i] = False\n        ans = []\n        visited = [False] * len(nums)\n        nums.sort()\n        backtrack([], len(nums))\n        return ans\n```\n\n**60. Permutation Sequence**\nhttps://leetcode.com/problems/permutation-sequence/\n```\n    def getPermutation(self, n, k):\n        nums = [str(i) for i in range(1, n+1)]\n        fact = [1] * n\n        for i in range(1,n):\n            fact[i] = i*fact[i-1]\n        k -= 1\n        ans = []\n        for i in range(n, 0, -1):\n            id = k / fact[i-1]\n            k %= fact[i-1]\n            ans.append(nums[id])\n            nums.pop(id)\n        return ''.join(ans)\n```\n\n**131. Palindrome Partitioning**\nhttps://leetcode.com/problems/palindrome-partitioning/\n```\n    def partition(self, s):\n        def backtrack(start, end, tmp):\n            if start == end:\n                ans.append(tmp[:])\n            for i in range(start, end):\n                cur = s[start:i+1]\n                if cur == cur[::-1]:\n                    tmp.append(cur)\n                    backtrack(i+1, end, tmp)\n                    tmp.pop()\n        ans = []\n        backtrack(0, len(s), [])\n        return ans\n```\n\n****\n\n\n**267. Palindrome Permutation II**\nhttps://leetcode.com/problems/palindrome-permutation-ii/\nRelated to this two:\n`31. Next Permutation`: https://leetcode.com/problems/next-permutation/\n`266. Palindrome Permutation`: https://leetcode.com/problems/palindrome-permutation/\n\n```\n    def generatePalindromes(self, s):\n        kv = collections.Counter(s)\n        mid = [k for k, v in kv.iteritems() if v%2]\n        if len(mid) > 1:\n            return []\n        mid = '' if mid == [] else mid[0]\n        half = ''.join([k * (v/2) for k, v in kv.iteritems()])\n        half = [c for c in half]\n        \n        def backtrack(end, tmp):\n            if len(tmp) == end:\n                cur = ''.join(tmp)\n                ans.append(cur + mid + cur[::-1])\n            else:\n                for i in range(end):\n                    if visited[i] or (i>0 and half[i] == half[i-1] and not visited[i-1]):\n                        continue\n                    visited[i] = True\n                    tmp.append(half[i])\n                    backtrack(end, tmp)\n                    visited[i] = False\n                    tmp.pop()\n                    \n        ans = []\n        visited = [False] * len(half)\n        backtrack(len(half), [])\n        return ans\n```
[]