@sk_3003 line 4 should be a = a*5
J
Jixin_Si_ND
@Jixin_Si_ND
0
Reputation
5
Posts
47
Profile views
0
Followers
0
Following
Posts made by Jixin_Si_ND

Python 53ms recursion solution with comments
This should be faster with a better 'pointers' movement strategy (e.g. increments based on candidate elements), especially when the target is large, but the basic idea of the algorithm is already here.
class Solution(object): def combinationSum2(self, candidates, target): """ :type candidates: List[int] :type target: int :rtype: List[List[int]] """ candidates.sort() self.solution = list() def helper(candidates, left, right, res): i = left # left pointer j = right  left # right pointer candidates2 = list() candidates2[:] = candidates # copy candidate list while i <= j: if i in candidates2: candidates2.remove(i) # remove already used/found candidates, the following recursion calls make sure all possible resolution start with the removed candidate get appended to the solution list. if j in candidates2: candidates2.remove(j) self.solution.append(res+ [i, j]) # add new resolution helper(candidates2, i, j, res + [i]) else: helper(candidates2, i, j, res + [i]) i += 1 # moving two pointers so that the summation == new target j = 1 left = 1 right = target if target in candidates: self.solution.append([target]) helper(candidates, left, right, []) return self.solution

Python solution according to approach 2 in the solution
class Solution(object): def nextPermutation(self, nums): """ :type nums: List[int] :rtype: void Do not return anything, modify nums inplace instead. """ if len(nums) == 1: return i = len(nums)  1 while i > 0: if nums[i] > nums[i  1]: # look for the first ascedent incident backwardly AJ = nums[i] # look for the 'just' larger than a[i  1] element in a[i:len(nums)] j = i for m in range(i, len(nums)): if nums[m] <= AJ and nums[m] > nums[i1]: AJ = nums[m] j = m nums[j], nums[i  1] = nums[i  1], nums[j] # switch nums[i:] = nums[len(nums)  1:(i  1):1] # reverse new a[i:len(nums)] return i = i  1 else: nums[:] = nums[::1] # reverse the whole list return

RE: Python solutions (iteratively, recursively, iteratively inplace).
@psnr You need to keep the head node for return, which is done by the dummy one, since cur will move to the tail of the merged list.