# Why LeetCode gives different result compared with the result from my laptop?

• Hi,

I have a question about my python code. When I run my code in my laptop (Mac OS) with testing case [1,3,2], it gives the correct answer = [2,1,3]. However, when I test my code in LeetCode, it gives a wrong answer = [2,3,1]. Could anyone know what causes this difference?

Below is my Python code for the NextPermutation problem:

``````class Solution(object):
def nextPermutation(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
for partition_idx in reversed(xrange(len(nums))):
if partition_idx == 0:
break
else:
if nums[partition_idx-1] < nums[partition_idx]:
break

if partition_idx == 0:
# original array is in descending order (the maximum permutation)
nums = sorted(nums)
else:
for pair_idx in reversed(xrange(len(nums))):
if nums[pair_idx] > nums[partition_idx-1]:
break

nums[partition_idx-1], nums[pair_idx] = nums[pair_idx], nums[partition_idx-1]
nums = nums[:partition_idx] + sorted(nums[partition_idx:])
``````

Thank you very much!

• Those `nums = ...` assignments make no sense. They only affect your local variable, and the caller won't able to see it at all. Did you mean to write `nums[:] = ...`? (Or in the first case, `nums.sort()`).

This definitely didn't work on your laptop, either. You must have tested wrong. Like print `nums` from the inside of the function at the end.

• I just checked that if I use "nums[:] = ...", then it will pass both in LeetCode and my mac. My previous code with "nums = ..." still passes in my mac and gives the correct answer.

I think the difference between "nums[:] = ..." and "nums = ..." is exactly what you said and causing the problem. Thank you very much for your suggestions! :)

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