0

While Trying to solve few question from LeetCode I am facing a really weird issue.

Question 26: Remove Duplicates from Sorted Array

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.

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.

enter image description here

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/

Rotate an array of n elements to the right by k steps.

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].

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.

2
  • Please view this thread hopefully it will solve your problem Sleep/Resume Scripts Ubuntu Commented Feb 8, 2017 at 12:25
  • Please ignore the above comment and answer, I just updated an old question. Commented Jan 13, 2018 at 0:02

3 Answers 3

1

Turns out the solution to this is to use: 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.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use sudo service network-manager restart

Comments

0

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. So, the requirement off the problem was the length you returned will be traveled from index 0 to the length returned. Hence with returning the length of unique values, we also have to modify the original list i.e., 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.