Skip to content

Commit 43e47dd

Browse files
authored
Create Apply Operations to an Array.java
1 parent 35b369d commit 43e47dd

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int[] applyOperations(int[] nums) {
3+
for (int i = 0; i < nums.length - 1; i++) {
4+
if (nums[i] == nums[i + 1]) {
5+
nums[i] *= 2;
6+
nums[i + 1] = 0;
7+
}
8+
}
9+
int startIdx = 0;
10+
int endIdx = 0;
11+
while (endIdx < nums.length) {
12+
if (nums[endIdx] != 0) {
13+
nums[startIdx++] = nums[endIdx];
14+
}
15+
endIdx++;
16+
}
17+
while (startIdx < nums.length) {
18+
nums[startIdx++] = 0;
19+
}
20+
return nums;
21+
}
22+
}

0 commit comments

Comments
 (0)