-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_2460.java
32 lines (31 loc) · 1.02 KB
/
_2460.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.fishercoder.solutions.thirdthousand;
public class _2460 {
public static class Solution1 {
public int[] applyOperations(int[] nums) {
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] == nums[i + 1]) {
nums[i] *= 2;
nums[i + 1] = 0;
}
}
// i points at the first zero element, j keeps moving and bypassing zeroes to find the
// next non-zero element
for (int i = 0, j = 0; i < nums.length && j < nums.length; ) {
while (i < nums.length && nums[i] != 0) {
i++;
}
if (j < i) {
j = i;
}
while (j < nums.length && nums[j] == 0) {
j++;
}
if (j < nums.length) {
nums[i++] = nums[j];
nums[j++] = 0;
}
}
return nums;
}
}
}