g2401_2500.s2460_apply_operations_to_an_array.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java11 Show documentation
Show all versions of leetcode-in-java11 Show documentation
Java Solution for LeetCode algorithm problems, continually updating
The newest version!
package g2401_2500.s2460_apply_operations_to_an_array;
// #Easy #Array #Simulation #2022_12_20_Time_1_ms_(87.93%)_Space_42.4_MB_(89.13%)
public class Solution {
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;
}
}
int index = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 0) {
nums[index] = nums[i];
index++;
}
}
for (int i = index; i < nums.length; i++) {
nums[i] = 0;
}
return nums;
}
}