LeetCode: Duplicate Zeros Solution

1/**
2 * @param {number[]} arr
3 * @return {void} Do not return anything, modify arr in-place instead.
4 */
5var duplicateZeros = function (arr) {
6 const N = arr.length
7 for (let i = 0; i < N - 1; i++) {
8 if (arr[i] === 0) {
9 let prev = arr[i + 1]
10 for (let j = i + 2; j < N; j++) {
11 let temp = arr[j]
12 arr[j] = prev
13 prev = temp
14 }
15 arr[++i] = 0
16 }
17 }
18}

Comments

Loading comments...

Tags

leetcode

array

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

LeetCode: Longest Common Subsequence

Classic dynamic programming problem

HoningJS

Search Posts