This is problem 26 in Leetcode.
When I return the result as an integer, they said the expected output as an array.
When I return the result as an array, they said it's TypeError because I need to return an integer.
My code with return statement as integer: https://paste.pythondiscord.com/DTFA
My code with return statement as array: https://paste.pythondiscord.com/O2KA
#๐ What do leetcode want me to return???
29 messages ยท Page 1 of 1 (latest)
@wind torrent
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
this is the problem right? https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/
it looks to me like it wants you to modify the list, but to also return a separate integer from the function
yeah?
i also modified the list btw
nums = [min(nums) + i for i in range(counter)]
plus i checked in other interpreter alr
its the problem
when the code's being ran and tested, leetcode is keeping hold of that nums list, and is checking its values after the function runs, because they're expecting you to change that list
but when you do nums = whatever, you aren't editing that original list, you're instead actually creating a brand new list
so long as at no point you do nums =, then yeah, it counts ๐
(you can also do nums[i] =, or call any methods, but just never specifically nums =)
then why in the original code, it says my output is [1, 1]?
wow it got me wrong again. its example consists of consecutive numbers!
see what the question says for example 1, which is the same as that input
you got given an input list of [1, 1, 2], and leetcode expected for that list to be changed to start with [1, 2], basically
yeah so i see 1 and 2
i thought that only return consecutive outputs
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
new = list(set(nums))
new.sort()
counter = 1
pointer = 0
expected_number = min(nums)
while expected_number in new:
counter += 1
pointer += 1
expected_number = new[pointer]
# Clears the list (setup)
for i in range(len(nums)):
nums.pop(0)
# Add the values
for i in range(counter):
nums.append(new[i])
return counter
i hope this works
oh nvm
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
new = list(set(nums))
new.sort()
# Clears the list (setup)
for i in range(len(nums)):
nums.pop(0)
# Add the values
for i in range(len(new)):
nums.append(new[i])
return len(new)
this works just like you said
thanks ๐
!close
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.