-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Update 283-Move-Zeroes.js #1596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Added solution with linear space just for reference's sake.
javascript/283-Move-Zeroes.js
Outdated
|
||
for (let i = 0; i < nums.length; i++) { | ||
if (nums[i] === 0) { | ||
right--; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add continue an remove else branch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aakhtar3, I updated the code, we don't need else or the continue part now.
Updated the logic. We don't need the "else" part nor do we require "continue".
javascript/283-Move-Zeroes.js
Outdated
const zeroAtTheEnd = Array(nums.length).fill(0); | ||
let left = 0; | ||
for (let i = 0; i < nums.length; i++) { | ||
if (nums[i]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should define your if conditions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aakhtar3, What do you mean by defining if conditions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Look at the Space O(1) if condition
javascript/283-Move-Zeroes.js
Outdated
|
||
const zeroAtTheEnd = Array(nums.length).fill(0); | ||
let left = 0; | ||
for (let i = 0; i < nums.length; i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of the solutions for 2 pointer, sliding window, and cyclic sort are in while loop. Easier to remember that pattern over for loop
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @aakhtar3, check now. Is this ok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do this
const arr = new Array(nums.length).fill(0);
let [ left, right ] = [ 0, 0 ];
while (right < nums.length) {
const isZero = (nums[right] === 0);
if (!isZero) {
arr[left] = nums[right];
left++;
}
right++;
}
return arr;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That could work, let me try to submit it on leetCode.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, shoot! we won't be able to because we're creating a new array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I did try some test cases and it seems to run just fine. I'll update the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check now @aakhtar3
Added a descriptive if condition.
Updated the code as suggested.
javascript/283-Move-Zeroes.js
Outdated
@@ -1,3 +1,29 @@ | |||
/** | |||
* Linear Time |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two Pointer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
javascript/283-Move-Zeroes.js
Outdated
@@ -1,3 +1,29 @@ | |||
/** | |||
* Linear Time | |||
* Time Complexity O(N) | Space Complexity O(N); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Time O(N) | Space O(N)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
updating solution description.
@aakhtar3, I update the file with the suggested changes. Please have a look. |
Added solution with linear space just for reference's sake.
File(s) Modified: 283-Move-Zeroes.js
Language(s) Used: JavaScript