Skip to content

Commit fba9ecc

Browse files
committedApr 3, 2025
Add solution #1089
1 parent 15934f4 commit fba9ecc

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,128 LeetCode solutions in JavaScript
1+
# 1,129 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -869,6 +869,7 @@
869869
1079|[Letter Tile Possibilities](./solutions/1079-letter-tile-possibilities.js)|Medium|
870870
1080|[Insufficient Nodes in Root to Leaf Paths](./solutions/1080-insufficient-nodes-in-root-to-leaf-paths.js)|Medium|
871871
1081|[Smallest Subsequence of Distinct Characters](./solutions/1081-smallest-subsequence-of-distinct-characters.js)|Medium|
872+
1089|[Duplicate Zeros](./solutions/1089-duplicate-zeros.js)|Easy|
872873
1092|[Shortest Common Supersequence](./solutions/1092-shortest-common-supersequence.js)|Hard|
873874
1103|[Distribute Candies to People](./solutions/1103-distribute-candies-to-people.js)|Easy|
874875
1108|[Defanging an IP Address](./solutions/1108-defanging-an-ip-address.js)|Easy|

‎solutions/1089-duplicate-zeros.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 1089. Duplicate Zeros
3+
* https://leetcode.com/problems/duplicate-zeros/
4+
* Difficulty: Easy
5+
*
6+
* Given a fixed-length integer array arr, duplicate each occurrence of zero, shifting the
7+
* remaining elements to the right.
8+
*
9+
* Note that elements beyond the length of the original array are not written. Do the above
10+
* modifications to the input array in place and do not return anything.
11+
*/
12+
13+
/**
14+
* @param {number[]} arr
15+
* @return {void} Do not return anything, modify arr in-place instead.
16+
*/
17+
var duplicateZeros = function(arr) {
18+
let zerosToAdd = 0;
19+
20+
for (let i = 0; i < arr.length; i++) {
21+
if (arr[i] === 0) zerosToAdd++;
22+
}
23+
24+
for (let i = arr.length - 1; i >= 0; i--) {
25+
if (arr[i] === 0) {
26+
zerosToAdd--;
27+
if (i + zerosToAdd + 1 < arr.length) {
28+
arr[i + zerosToAdd + 1] = 0;
29+
}
30+
}
31+
if (i + zerosToAdd < arr.length) {
32+
arr[i + zerosToAdd] = arr[i];
33+
}
34+
}
35+
};

0 commit comments

Comments
 (0)
Please sign in to comment.