File tree 2 files changed +37
-1
lines changed
2 files changed +37
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,128 LeetCode solutions in JavaScript
1
+ # 1,129 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
869
869
1079|[ Letter Tile Possibilities] ( ./solutions/1079-letter-tile-possibilities.js ) |Medium|
870
870
1080|[ Insufficient Nodes in Root to Leaf Paths] ( ./solutions/1080-insufficient-nodes-in-root-to-leaf-paths.js ) |Medium|
871
871
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|
872
873
1092|[ Shortest Common Supersequence] ( ./solutions/1092-shortest-common-supersequence.js ) |Hard|
873
874
1103|[ Distribute Candies to People] ( ./solutions/1103-distribute-candies-to-people.js ) |Easy|
874
875
1108|[ Defanging an IP Address] ( ./solutions/1108-defanging-an-ip-address.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments