# 2460. Apply Operations to an Array ###### tags: `Leetcode` `Easy` `Two Pointers` Link: https://leetcode.com/problems/apply-operations-to-an-array/description/ ## 思路 $O(N)$ $O(1)$ 首先brute force 如果```nums[i]==nums[i+1]```, ```nums[i]*=2;``` ```nums[i+1]=0;``` 然后用双指针shift 0 ## Code ```java= 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 p1=0, p2=0; for(p1=0; p1<nums.length; p1++){ if(nums[p1]!=0){ nums[p2++] = nums[p1]; } } while(p2<nums.length){ nums[p2++] = 0; } return nums; } } ```