Skip to content

Commit bf77eab

Browse files
solves duplicate zeros
1 parent 08cb786 commit bf77eab

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,9 @@
291291
| 1065 | 🔒 [Index Pairs of a String](https://leetcode.com/problems/index-pairs-of-a-string) | | |
292292
| 1071 | [Greatest Common Divisors of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings) | [![Java](assets/java.png)](src/GreatestCommonDivisorOfStrings.java) | |
293293
| 1078 | [Occurrence After Bigram](https://leetcode.com/problems/occurrences-after-bigram) | [![Java](assets/java.png)](src/OccurrencesAfterBigram.java) | |
294-
| 1085 | [Sum of Digits in Minimum Number](https://leetcode.com/problems/sum-of-digits-in-the-minimum-number) | | |
295-
| 1086 | [High Five](https://leetcode.com/problems/high-five) | | |
296-
| 1089 | [Duplicate Zeroes](https://leetcode.com/problems/duplicate-zeros) | | |
294+
| 1085 | 🔒 [Sum of Digits in Minimum Number](https://leetcode.com/problems/sum-of-digits-in-the-minimum-number) | | |
295+
| 1086 | 🔒 [High Five](https://leetcode.com/problems/high-five) | | |
296+
| 1089 | [Duplicate Zeroes](https://leetcode.com/problems/duplicate-zeros) | [![Java](assets/java.png)](src/DuplicateZeros.java) | |
297297
| 1099 | [Two Sum Less Than K](https://leetcode.com/problems/two-sum-less-than-k) | | |
298298
| 1103 | [Distribute Candies to People](https://leetcode.com/problems/distribute-candies-to-people) | | |
299299
| 1108 | [Defanging an IP Address](https://leetcode.com/problems/defanging-an-ip-address) | | |

src/DuplicateZeros.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class DuplicateZeros {
2+
public void duplicateZeros(int[] array) {
3+
int removals = 0, k = array.length - 1;
4+
for (int i = 0 ; i < array.length - removals ; i++) {
5+
if (array[i] == 0) {
6+
if (i == array.length - 1 - removals) {
7+
array[k--] = 0;
8+
}
9+
removals++;
10+
}
11+
}
12+
for (int i = array.length - 1 - removals ; i >= 0 ; i--, k--) {
13+
if (array[i] == 0) {
14+
array[k] = array[k - 1] = 0;
15+
k--;
16+
} else {
17+
array[k] = array[i];
18+
}
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)