I have been trying to do the following problem on leetcode
I did solve it - and when I am printing out my results, it is correct. However, the output of the problem is still incorrect (though it is the same variable...) I think it is because the problem asks to do everything in-place - and I assumed I did it.
My code is here:
class Solution:
def nextPermutation(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
k = len(nums) - 1
i = - 100
while k > 0:
if nums[k - 1] < nums[k]:
i = k
break
else:
k -=1
if i > 0:
iterator = i - 1
while iterator <= len(nums) - 1:
if iterator == len(nums) - 1 or (nums[iterator] > nums[i-1] and nums[iterator + 1] < nums[i-1] ):
nums = self.swap(nums,i-1,iterator)
iterator += 1
nums = nums[:i] + sorted(nums[i:])
print(nums) #HERE THE PRINTED ANSWER IS CORRECT
else:
nums.sort()
return
def swap(self,nums,i,j):
temp = nums[i]
nums[i] = nums[j]
nums[j] = temp
return nums
The output I am getting:
So my main question is why the output value and the stout of the same variable are different? Is there a way to fix it?