All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2401_2500.s2460_apply_operations_to_an_array.Solution Maven / Gradle / Ivy

There is a newer version: 1.37
Show 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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy