Guys please help me to find out where goes wrong using my solution? Thanks you very much!


  • -1
    Y
    def rotate(self, nums, k):
        if k==0:
            return nums
        else:
            if len(nums)==1:
               return nums
            else:
              l2=[0]*len(nums)
              x=k%(len(nums))
              for i in range(len(nums)):
                if (i+x)<len(l2):
                   l2[i+x]=nums[i]
                else:
                   l2[i+x-len(l2)]=nums[i]
              return l2

  • 1

    Yeah, where does it go wrong? The OJ tells you (input, output, expected), so why don't you tell us? It would help!

    Anyway, you're making the common mistake of not modifying the given list but creating a new list that the OJ doesn't care about. They really mean it when they specify the result as:

    @return {void} Do not return anything, modify nums in-place instead.


Log in to reply
 

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