What's wrong with my python code?

  • 0

    In my code, i is the index move from low digit to higher. j is the index to digits lower than i.
    At first, it finds the index maxID with smallest value but higher than i,
    if find such maxID, then
    swap the maxID and i th data in the given list, and
    adjust the following digits to make it as small as possible
    return the list with the smallest value

    And I got the "Runtime Error Message: Line 58: TypeError: nextPermutation() takes exactly 1 argument (2 given) when Last executed input:[1]"

    I can run my code in my computer. Please help me to check my code. Thanks a lot.

    class Solution:
        # @param num, a list of integer
        # @return a list of integer
        def nextPermutation(num):
            for i in reversed(range(len(num))):
                maxID = i
                if i == len(num) - 1:
                for j in reversed(range(i+1,len(num))):
                    if maxID==i and num[maxID] < num[j]:
                        maxID = j
                    if maxID !=i and num[maxID] > num[j] and num[i] < num[j]:
                        maxID = j
                if maxID == i:
                    t = num[i]
                    num[i] = num[maxID]
                    num[maxID] = t
                    ##minimize following
                    for v in range(i+1,len(num)):
                        minID = v
                        if v == len(num)-1:
                        for u in range(v+1,len(num)):
                            if num[minID] > num[u]:
                                minID = u
                        if minID == v:
                            t = num[v]
                            num[v] = num[minID]
                            num[minID] = t
                    return num
            for i in range(len(num)):
                minID = i
                if i == len(num) - 1:
                for j in range(i+1,len(num)):
                    if num[minID] > num[j]:
                        minID = j
                if minID == i:
                    t = num[i]
                    num[i] = num[j]
                    num[j] = t
            return num

  • 0

    you must have modified "def permutation(num)" which should be "def permutation(self, num)"

Log in to reply

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