LeetCode Remove Element Solution Explained - Java

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

My fucking God. I was way over complicating this. I got wrapped around the axle trying to get the hint#2 method to work. The one where if you come across the target value you swap it with the end of the array. My output was all over the place. I need a moment. I want to put my head through my computer screen after seeing how simple this is. I have never wanted to quit coding as much as i do right now.

mikembsc
Автор

I never comment, but you are the best! So I cant stop myself to comment you to say you are the best and most clear explanation I have ever seen.

Ivan-ekkg
Автор

why i can't understand 😭 I'm so bad when it's come to loop 😭

sheldoncooper
Автор

It's not weird, appreciate the love man and we love you too. Hopefully, this practice is just what I need for my technical interview

darvyntalieh
Автор

Its been a month I have started my learning DSA journey and solving problems and I am still not able to think something simple like this. I was much more focused on if(nums[i] == val) which was making it more difficult and complex for me. @Nick White can you help me and tell me if i am on the right path. Id love to hear your suggestion.

sauravsrivastav
Автор

What tripped me up in the problem description is that it doesn't say we can move the values to the back of the array, rather in the example they just leave blanks, "_". And they want us to use the in place array. Apparently in java we can't set primitive values to null, in other problem examples they set empty values to zero....

A super easy problem, but idk why they show a blank space where the val elements would go

alastairhewitt
Автор

thanks nick, for the wonderful explanation.

The way you say it's easy, that confidence help to look at it without any fear.
thanks for that as well.

akware
Автор

what happens to the array...when you move the non target value to the index[j]...what happens to the position where the non target element initially was ?cause we are not swapping ..right? please somebody explain

izhanmasoodbaba
Автор

"couldn't be easier to solve" "yeah an easy problem" - meanwhile me in my second day (2 h first day) trying to wrap my brain around it... :))) thanks Nick! not helping the cause, buddy! lol

SosetaFurioasaJr
Автор

Oh this was very easy compared to my solution which uses two for loops one for sorting and other for counting the no of values thanks

vishnu
Автор

Hey I'm new to this so how does return valid_size give a value of say [x, x] as the output?

nithinss
Автор

public class Solution {
public int RemoveElement(int[] nums, int val) {

int left =0;
int right = nums.Length-1;

while(left<=right){

if(nums[left]==val)
{
if(nums[right]!=val)
{
nums[left] = nums[right];
nums[right] = val;
left++;
right--;
}else
{
right--;
}
}else{
left++;
}

}

return left;

}
}

mohammedghabyen
Автор

Hope that you can put the problem number on the title for easily finding. Thanks

raferguo
Автор

public int removeElement(int[] nums, int val) {
int N = nums.length;
int L = 0, R = 1;
if (N == 1) {
R = 0;
}
while (R < N) {
if (nums[L] != val) {
L++;
R++;
} else if (nums[R] == val) {
R++;
} else {
int temp = nums[L];
nums[L] = nums[R];
nums[R] = temp;
}

}
return L;

}

Okay so i orriginally had this but was failing a couple of test cases where the array had the all the same elements, then i looked at your solution made R = 0, and got it right, im not quite sure why but im going to investigate and go thru examples.

Rob-J-BJJ
Автор

thank you so much . perfect lesson algorithms

cba
Автор

Hi bro. Can you give a tip for cracking product based company like Facebook? And how to start preparing problem solving from beginner to pro

vasanthakumarn
Автор

very good explanation, but still very confusing question hahaha

hellnuker
Автор

Wtf I literally wasted so much time to solve this problem without using extra space
I was doing shifting the numbers like that

nileshkharatmol
Автор

This was hard for me, I did nums.remvoe(val) and then I realized that's the wrong way of doing it. UGh this was not easy for me =(

Historyiswatching