简体   繁体   English

Leetcode Python问题

[英]Leetcode Python Issue

While Trying to solve few question from LeetCode I am facing a really weird issue. 在尝试解决LeetCode的一些问题时,我面临着一个非常奇怪的问题。

Question 26: Remove Duplicates from Sorted Array 问题26:从排序数组中删除重复项

https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/ https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/

Example:

Given nums = [1,1,2],

Your function should return length = 2, with the first two elements of 
nums being 1 and 2 respectively.
It doesn't matter what you leave beyond the new length.

In order to code this question I used : 为了编码这个问题,我使用了:

class Solution(object):
def removeDuplicates(self, nums):
    nums = list(set(nums))
    return len(nums)

what this code is doing is first converting the list into a set and then back to list, which in turn will remove the Duplicates 这段代码要做的是先将列表转换为集合,然后再返回列表,这将删除重复项

But when I am trying to submit this code to the leetcode solution, modified length of nums is returned but when the program is trying to access the nums array it is not updated. 但是,当我尝试将此代码提交给leetcode解决方案时,将返回已修改的nums长度,但是当程序尝试访问nums数组时,它不会更新。

This is only Happening in Leetcode editor, in my system If I try to print the nums, the modified value is displayed, not sure what is wrong. 这仅在我的系统中的Leetcode编辑器中发生。如果我尝试打印num,则会显示修改后的值,不确定是什么错误。

在此处输入图片说明

Now the same case is Happening to other question as well, for example: 现在,同样的情况也发生在其他问题上,例如:

Rotate Array https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/646/ 旋转阵列https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/646/

Rotate an array of n elements to the right by k steps. 将k个元素向右旋转n个元素的数组。

For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 例如,在n = 7和k = 3的情况下,数组[1,2,3,4,5,6,7]旋转为[5,6,7,1,2,3,4]。

My solution to this problem is as follows: 我对这个问题的解决方法如下:

class Solution(object):
 def rotate(self, nums, k):
    newIndex = k % len(nums)
    nums = nums[newIndex+1:len(nums)] + nums[0:newIndex+1]
    print nums

But again I am amazed by the output I am getting back from the submission. 但是我再次对提交的输出感到惊讶。

Note Here in the "Your STDOUT" we can see the list is modified accordingly. 注意在这里,在“您的标准输出”中,我们可以看到列表已被相应修改。 link to the Screenshot 链接到截图

Please let me know if anyone else is facing this issue or anyone knows the solution to this. 如果有人遇到此问题或知道解决方案,请告诉我。

您可以使用sudo service network-manager restart

Turns out the solution to this is to use: nums[:] = nums[newIndex+1:len(nums)] + nums[0:newIndex+1] . 原来的解决方案是使用: nums[:] = nums[newIndex+1:len(nums)] + nums[0:newIndex+1]

Doing nums = nums[newIndex+1:len(nums)] + nums[0:newIndex+1] simply changes the reference, while nums[:] changes the values of the list. 执行nums = nums[newIndex+1:len(nums)] + nums[0:newIndex+1]只会更改引用,而nums[:]更改列表的值。

what was happening in your code is the length you were returning is been used to travel the nums in back-end to print the unique values of the nums list. 您的代码中发生的是返回的长度用于在后端移动nums以打印nums列表的唯一值。 So, the requirement off the problem was the length you returned will be traveled from index 0 to the length returned. 因此,问题的要求是您返回的长度将从索引0移到返回的长度。 Hence with returning the length of unique values, we also have to modify the original list ie, nums . 因此,在返回唯一值的长度时,我们还必须修改原始列表,即nums Solution for the 1st link 第一个链接的解决方案

class Solution:
    def removeDuplicates(self, nums):
        if(len(nums) == 0):
            return 0
        elif len(nums) == 1 :
            return 1
        else:
            l = 1
            for i in range (1,len(nums)):
                if nums[i] != nums[i-1] :
                    #l+=1
                    nums[l] = nums[i]
                    l+=1
            return l

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM