Skip to content

Commit 88ecb65

Browse files
authored
Update Apply Operations to an Array.java
1 parent c25f01b commit 88ecb65

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

Easy/Apply Operations to an Array.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
class Solution {
22
public int[] applyOperations(int[] nums) {
3-
for (int i = 0; i < nums.length - 1; i++) {
3+
int n = nums.length;
4+
for (int i = 0; i < n - 1; i++) {
45
if (nums[i] == nums[i + 1]) {
56
nums[i] *= 2;
67
nums[i + 1] = 0;
78
}
89
}
9-
int startIdx = 0;
10-
int endIdx = 0;
11-
while (endIdx < nums.length) {
12-
if (nums[endIdx] != 0) {
13-
nums[startIdx++] = nums[endIdx];
10+
int start = 0;
11+
for (int i = 0; i < n; i++) {
12+
if (nums[i] != 0) {
13+
nums[start++] = nums[i];
1414
}
15-
endIdx++;
1615
}
17-
while (startIdx < nums.length) {
18-
nums[startIdx++] = 0;
16+
while (start < n) {
17+
nums[start++] = 0;
1918
}
2019
return nums;
2120
}

0 commit comments

Comments
 (0)