Комментарии:
did this on my own after checking your other vids, thanks to you Neetcode!
ОтветитьCan we say that it is some way a sliding window?
Ответитьwhy using set gives wrong answer?
it prints the correct value in stdout but wrong in output
def removeDuplicate(n):
l = 1
for r in range(1, len(n)):
if n[r] != n[l]:
l += 1
n[l] = n[r]
return l+1
This problem is written like shit. This comment helped solve it without watching the solution:
"They don't really want you to remove the duplicates. They want you to sort the uniques at the front, then return the length of the sorted part. Then, behind the scenes, they slice the array at the length you give them and the result of that is what they check.
Just FYI, this sh_t drove me crazy..."
If input is [1,1,2] it will do [1,2,2] and l=2
So print [1,2]
But expected output is [1,2,_]
To do this you need to create new array
It not delete just reassign value
Ответитьif array like [0 1 2 2 3] and we are starting from index 1 then r # r+1 so we lose the value 1 cuz 2#1, can someone explain me pls
Ответитьwhen i put the same code you did I still didn't get the write answer
ОтветитьIn my opinion, how about we used sort function? Answer : unique_arr = list(set(arr))
ОтветитьWhy is this code not working for the solution?
class Solution:
def removeDuplicates(self, nums):
uniqueset = set()
for n in nums:
uniqueset.add(n)
return len(uniqueset)
I've tried this outside of LeetCode's checkers and it returns an integer of the expected length. Doing print(uniqueset) returns a set of the expected values, but Leetcode's checker says it returns a completely different list of values. I'm very confused as to what has gone wrong, but I am new to Python3.
i did not understand when i try to use hash set and return the count of hash leetcode return me a error can we solve this problem with hash set? if its not why?
ОтветитьMy test cases were accepted but my final submission didn’t get accepted because it exceeded the time limit. That was just a clear reflection of how I code, inefficient but getting the job done. My house is burning but hey, at least we got heat.
ОтветитьHaving a third pointer specifically for maintaing the unique value is much easier and more understandable thhan managing with two pointers
ОтветитьNo one can replace you❤
ОтветитьОХуенно!
ОтветитьThe LeetCode instructions were absolutely awful for this question. They rarely seem to explain stuff in simple terms, it's so annoying. Half the battle is deciphering the question itself, sometimes the code isn't even hard to come up with, just interpreting the question.
ОтветитьThank you! I was a bit confused because I thought you had to null all the other values in the updated array, because the question had _ representing those indices.
Ответитьpublic int removeDuplicates(int[] nums) {
int k = 1; // k is the left pointer
for (int i = 1; i < nums.length; i++) { // ? i is the right pointer
if (nums[i] != nums[i - 1])
nums[k++] = nums[i];
}
return k;
}
I came up with a crafty solution xD, but it's in O(n^2) !
ОтветитьAbout as efficient as it can get huh? But the LeetCode statistics aren't that reliable in terms of efficiency right?
ОтветитьI actually spend so much time solving it.. that too by shifting the elements but it could have done a much simpler way. I really don't know if I will ever get good enough to reach the point where the best solution just clicks after some thinking .it's so frustrating
Ответитьanyone could please send the program that i could execute on my ide with using the same logic
ОтветитьReally like your explanations. I'm using JavaScript, but you make it so easy to undestand the problem and the solution for it.
ОтветитьGreat man salute 😄nicely explained
ОтветитьThe array cant be empty cuz the first constraint in the question is (1 <= nums.length <= 3 * 104)
ОтветитьI hated this problem
ОтветитьYou just made it so easy!
Ответить"Return the number of unique values in the array" is SO much better than saying "Return k", thank you!
Ответитьcan someone please tell me what argument is -> int is
Ответитьhi, what drawing app are you using? thanks
Ответитьi don't understand why we can't use the pop function
ОтветитьOh my God... What an easy solution... And i was thinking all the complex way I can
Ответитьthanks
Ответитьeasy with numbers what about strings ?
ОтветитьSuch a clean and brilliant solution.
ОтветитьJava solution:
public static int removeDuplicates(int[] arr) {
int left = 1;
for (int right = 1; right < arr.length; right++) {
if (arr[right] != arr[right - 1]) {
arr[left] = arr[right];
left++;
}
}
return left;
}
Does anyone have problem running this code with zero or negative value's array on leet code simulation? Whenever i run array that has zero or negative value, the loop left out the very last value.
ОтветитьProblem seems to be very simple, but also seems to be written by an alien
ОтветитьWhy can't we just convert it to a set, and then convert it to a set and then return the length of the set?
Ответитьi put the same codes but its saying theres an error
Ответитьdef removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
x = 0
while x < len(nums):
val = nums[x]
if nums[x+1:].count(val) >= 1:
# nums.remove(val)
nums[x+1:] = list(filter(lambda a: a != val, nums[x+1:]))
else:
x += 1
return len(nums)
How is this approach?
not sure why i get an index out of bound getting the same output using golang
Ответитьthanks for good work...crystal clear explanation..
Ответитьdef removeDuplicates(self, nums: List[int]) -> int:
prev = None
j=0
for i in nums:
if i != prev:
nums[j] = i
j+=1
prev = i
return j
Thank you
Ответить