filmov
tv
LeetCode 26: Remove Duplicates from Sorted Array - Interview Prep Ep 45
Показать описание
LeetCode 26. Remove Duplicates from Sorted Array
⭐ Support my channel and connect with me:
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
Internally you can think of this:
// nums is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums);
// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i smaller than len; i++) {
print(nums[i]);
}
Solution explained:
1. We can apply the two pointer technique: one slow pointer pointing towards the one that's the last known distinct element, one fast pointer keeping moving towards the end of the array to scan for the next possible unique element.
2. Finally, we just need to return the slow pointer position plus one.
// TOOLS THAT I USE:
// MY FAVORITE BOOKS:
My ENTIRE Programming Equipment and Computer Science Bookshelf:
And make sure you subscribe to my channel!
Your comments/thoughts/questions/advice will be greatly appreciated!
#softwareengineering #leetcode #algorithms #coding #interview #SDE #SWE #SiliconValley #programming #datastructures
Комментарии