-
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,23 @@ | ||
/** | ||
* Linear Time | ||
* Time Complexity O(N) | Space Complexity O(N); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
* https://leetcode.com/problems/move-zeroes/ | ||
* @param {number[]} nums | ||
* @return {void} Do not return anything, modify nums in-place instead. | ||
*/ | ||
var moveZeroes = function(nums) { | ||
|
||
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 commentThe 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 commentThe 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 commentThe 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 commentThe 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 commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Please check now @aakhtar3 |
||
if (nums[i]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Look at the Space O(1) if condition |
||
zeroAtTheEnd[left] = nums[i]; | ||
left++; | ||
} | ||
} | ||
return zeroAtTheEnd; | ||
}; | ||
|
||
/** | ||
* 2 Pointer | ||
* Time O(N) | Space O(1) | ||
|
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