Skip to content

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

Merged
merged 5 commits into from
Dec 28, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions javascript/283-Move-Zeroes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
/**
* Linear Time
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two Pointer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

* Time Complexity O(N) | Space Complexity O(N);
Copy link
Collaborator

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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++) {
Copy link
Collaborator

@aakhtar3 aakhtar3 Dec 25, 2022

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

Copy link
Contributor Author

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?

Copy link
Collaborator

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;

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check now @aakhtar3

if (nums[i]) {
Copy link
Collaborator

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

Copy link
Contributor Author

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?

Copy link
Collaborator

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

zeroAtTheEnd[left] = nums[i];
left++;
}
}
return zeroAtTheEnd;
};

/**
* 2 Pointer
* Time O(N) | Space O(1)
Expand Down